Correspondence of the event that the result of form.is_valid () is always False in Django2 system

Events and environment

Event

** 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'})
        }

environment

The execution environment is ** django2.2.12 **, python3.7, Windows10.

Cause

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.

Correspondence

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.

reference

https://stackoverflow.com/questions/20801452/django-form-is-valid-always-false

Recommended Posts

Correspondence of the event that the result of form.is_valid () is always False in Django2 system
The meaning of ".object" in Django
The result of installing python in Anaconda
View the result of geometry processing in Python
The story of viewing media files in Django
[Django] Let's try to clarify the part of Django that was somehow through in the test
Here is one of the apps with "artificial intelligence" that I was interested in.
Linux is something like that in the first place
Code that sets the default value in case of AttributeError
I participated in the translation activity of Django official documents
Measure the execution result of the program in C ++, Java, Python.
The result of Java engineers learning machine learning in Python www
Django + MongoDB development environment maintenance (in the middle of writing)
Summary of stumbling blocks in Django for the first time
Display the timetable of the Morioka bus location system in Pythonista
Is there a bias in the numbers that appear in the Fibonacci numbers?
It is said that Fabric cannot get the status code in the middle of the pipe in the shell script.
Python Priority Queuing The mystery of the result of the heapify function that most people wouldn't be interested in