Skip to content

Commit e3b487e

Browse files
44 - Form Validation on a Post Method
1 parent 5d009ef commit e3b487e

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/courses/forms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ class Meta:
88
model = Course
99
fields = [
1010
'title'
11-
]
11+
]
12+
13+
def clean_title(self):
14+
title = self.cleaned_data.get('title')
15+
if title.lower() == 'abc':
16+
raise forms.ValidationError("This is not a valid title")
17+
return title

src/courses/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def post(self, request, *args, **kwargs):
1818
form = CourseModelForm(request.POST)
1919
if form.is_valid():
2020
form.save()
21+
form = CourseModelForm()
2122
context = {"form": form}
2223
return render(request, self.template_name, context)
2324

0 commit comments

Comments
 (0)