File tree 3 files changed +44
-13
lines changed
3 files changed +44
-13
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
from django .contrib .auth .decorators import login_required
2
2
from django .shortcuts import render
3
3
4
+ from .forms import ArticleForm
4
5
from .models import Article
5
6
# Create your views here.
6
7
@@ -24,14 +25,19 @@ def article_search_view(request):
24
25
@login_required
25
26
def article_create_view (request ):
26
27
# print(request.POST)
27
- context = {}
28
+ form = ArticleForm ()
29
+ context = {
30
+ "form" : form
31
+ }
28
32
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
35
41
return render (request , "articles/create.html" , context = context )
36
42
37
43
def article_detail_view (request , id = None ):
Original file line number Diff line number Diff line change 4
4
{% block content %}
5
5
6
6
{% if not created %}
7
+
8
+
9
+
7
10
< div style ='margin-top:30px; '>
8
11
< form action ='. ' method ="POST ">
9
12
{% 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 }}
16
14
< button style ='margin-top:10px; ' type ='submit ' > Create article</ button >
17
15
</ form >
18
16
</ div >
You can’t perform that action at this time.
0 commit comments