1
You are writing function based view code inside a class based view, which is incorrect.
You shouldn’t need to instantiate the form, or manually check whether it is valid. Just set form_class
for the view, then override form_valid
or form_invalid
if you need to change the behaviour when the form is valid or invalid. Since you have {{ form.errors }}
in your template, it should show any errors when you submit the form.
class PostCreate(generic.CreateView):
form_class = PostForm
model = Post
See the docs on form handling with class based views for more information. You might find it easier to write a function based view to begin with, since the flow of the code is easier to follow.
Source:stackexchange.com