** is_valid ** was executed by the ** post ** method of ** View ** class written in ** views.py **. The result of ** is_valid ** is ** False ** no matter what input is made on the web browser.
The validation method is not implemented in forms.py.
The following is an excerpt of the source code. There is a ** Meta ** class in the ** Form ** class, but this is a recognition that has nothing to do with this event.
views.py
class View(View):
def post(self, request, *args, **kwargs):
form = Form()
is_valid = form.is_valid()
print(is_valid)
...
forms.py
class Form(forms.ModelForm):
class Meta:
model = ExperimentResult
fields = ("title", "comment",)
widgets = {
'title' : forms.TextInput(attrs={'class':'text_area'}),
'comment' : forms.TextInput(attrs={'class':'text_area'})
}
The execution environment is ** django2.2.12 **, python3.7, Windows10.
When instantiating the Form class, it is assumed that there is a "request" in the first argument of the init method.
When writing form = FormClass (), "request" is not passed to the init method. Therefore, when the "is_valid" method is executed, a validation error occurs because "request" is not passed, and False is always returned.
Insert data = request.POST as an argument when instantiating the Form class.
views.py
class View(View):
def post(self, request, *args, **kwargs):
form = Form(data=request.POST)
is_valid = form.is_valid()
print(is_valid)
...
After taking the above actions, I confirmed that True would return.
https://stackoverflow.com/questions/20801452/django-form-is-valid-always-false
Recommended Posts