Django Form Invalid, Despite No Errors

 4个月前 阅读数 5
以下为 快照 页面,建议前往来源网站查看,会有更好的阅读体验。
原文链接: https://snakeycode.wordpress.com/2019/07/08/django-form-invalid-despite-no-errors/


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.