14

Django Form Invalid, Despite No Errors

 3 years ago
source link: https://snakeycode.wordpress.com/2019/07/08/django-form-invalid-despite-no-errors/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Standard

Django Form Invalid, Despite No Errors

I was using Django CBV FormView. The form was being flagged as invalid but when I inspected the form inside the form_invalid() method using the PyCharm debugger, there were no errors. When I traced deeper into the Django code, I found this line that is returned by the is_valid() method.:

return self.is_bound and not self.errors

It turned out my form was not bound. But why would that be???

The problem was I over-rode the get_form_kwargs() method and forgot to call super(). The POST data is one of the kwargs passed to the form. If you don’t call super(), the post data does not make it into the form kwargs. Hence the form remains unbound.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK