[Solved]-Form.is_valid() returns false (django)

8👍

Every field in form are required by default (required=True). Form submitted without information in required field is not valid. You can add path field to your form in template and it must be filled or you can make path not required:

class SendFileForm(forms.Form):
    path = forms.CharField(required=False)
    ...

or

<form action={% url "sent" %} method="post" enctype="multipart/form-data">
...
            {{ form.songfile }}
            {{ form.path }}
...
</form>
👤ndpu

15👍

Note:- This answer is Only to help you in debugging the code.

You can print form errors in your views directly.

class YourFormView(generic.edit.CreateView):

  def post(self, request, *args, **kwargs):
    form = YourForm(request.POST)
    for field in form:
        print("Field Error:", field.name,  field.errors)
      

2👍

The problem is that there is no path input in your template. Your request.POST contains incomplete data thats why your form is not valid.

This is missing in your template:

{{ form.path }}
👤niekas

0👍

In addition to laxmikant’s answer, one more way to debug is get the errors like below,

class YourFormView(generic.edit.CreateView):

  def post(self, request, *args, **kwargs):
    form = YourForm(request.POST)
    print(form.errors)

Note: This is only debugging technique

Leave a comment