Skip to content

Commit f83514d

Browse files
27 - Basic Django Form
1 parent 3b884d1 commit f83514d

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

articles/forms.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django import forms
2+
3+
class ArticleForm(forms.Form):
4+
title = forms.CharField()
5+
content = forms.CharField()
6+
7+
# def clean_title(self):
8+
# cleaned_data = self.cleaned_data # dictionary
9+
# print("cleaned_data", cleaned_data)
10+
# title = cleaned_data.get('title')
11+
# if title.lower().strip() == "the office":
12+
# raise forms.ValidationError('This title is taken.')
13+
# print("title", title)
14+
# return title
15+
16+
def clean(self):
17+
cleaned_data = self.cleaned_data
18+
print('all data', cleaned_data)
19+
title = cleaned_data.get('title')
20+
content = cleaned_data.get("content")
21+
if title.lower().strip() == "the office":
22+
self.add_error('title', 'This title is taken.')
23+
# raise forms.ValidationError('This title is taken.')
24+
if "office" in content or "office" in title.lower():
25+
self.add_error('content', "Office cannot be in content")
26+
raise forms.ValidationError("Office is not allowed")
27+
return cleaned_data

articles/views.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib.auth.decorators import login_required
22
from django.shortcuts import render
33

4+
from .forms import ArticleForm
45
from .models import Article
56
# Create your views here.
67

@@ -24,14 +25,19 @@ def article_search_view(request):
2425
@login_required
2526
def article_create_view(request):
2627
# print(request.POST)
27-
context = {}
28+
form = ArticleForm()
29+
context = {
30+
"form": form
31+
}
2832
if request.method == "POST":
29-
title = request.POST.get("title")
30-
content = request.POST.get("content")
31-
print(title, content)
32-
article_object = Article.objects.create(title=title, content=content)
33-
context['object'] = article_object
34-
context['created'] = True
33+
form = ArticleForm(request.POST)
34+
context['form'] = form
35+
if form.is_valid():
36+
title = form.cleaned_data.get("title")
37+
content = form.cleaned_data.get("content")
38+
article_object = Article.objects.create(title=title, content=content)
39+
context['object'] = article_object
40+
context['created'] = True
3541
return render(request, "articles/create.html", context=context)
3642

3743
def article_detail_view(request, id=None):

templates/articles/create.html

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
{% block content %}
55

66
{% if not created %}
7+
8+
9+
710
<div style='margin-top:30px;'>
811
<form action='.' method="POST">
912
{% csrf_token %}
10-
<div>
11-
<input type='text' name='title' placeholder='Title' />
12-
</div>
13-
<div style='margin-top:10px;'>
14-
<textarea name='content' placeholder='Content'></textarea>
15-
</div>
13+
{{ form.as_p }}
1614
<button style='margin-top:10px;' type='submit' >Create article</button>
1715
</form>
1816
</div>

0 commit comments

Comments
 (0)