form.is_valid() returns false (django)

23,373

Solution 1

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>

Solution 2

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)
      

Solution 3

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 }}
Share:
23,373
Badi8beach
Author by

Badi8beach

Updated on May 01, 2021

Comments

  • Badi8beach
    Badi8beach about 3 years

    I am a bit new to django. Im trying to send a file over to another server once it is chosen in upload, but is form.is_valid() always return false would not let me enter if

    views.py-

    def sent(request):
        if request.method == 'POST':
            form = SendFileForm(request.POST, request.FILES)
            print "form is made"
            print form.errors
            if form.is_valid():
                print "form is valid"
                new_song = Song(songfile= request.FILES['songfile'])
                new_song.save()
                print "new song is made and saved"
                l = List()
                #cd = form.cleaned_data                                                                                                                   
                #SENDS THE FILE TO SERVER GIVEN PATH
                l.all_files(new_song.songfile.path)
                return HttpResponseRedirect(reverse('get_files.views.sent'))
            else:
                print "form is not valid"
        else:
            form = SendFileForm()
    
        songs = Song.objects.all()
        return render_to_response('sent.html', {'songs': songs,'form': form}, context_instance=RequestContext(request))
    

    sent.html template-

    {% if form.errors %}
        <p style="color: red;">
            Please correct the error{{ form.errors|pluralize }} below.
        </p>
    {% endif %}
    
    <form action={% url "sent" %} method="post" enctype="multipart/form-data">
      {% csrf_token %}
        <p>{{ form.non_field_errors }}</p>
            <p>{{ form.songfile.label_tag }} {{ form.songfile.help_text }}</p>
            <p>
                <!--{{ form.songfile.errors }}-->
                {{ form.songfile }}
            </p>
            <p><input type="submit" value="Upload" /></p>
    </form>
    

    forms.py-

    class SendFileForm(forms.Form):
        path = forms.CharField()
        songfile = forms.FileField(label='Select a music file',help_text='max. 4 megabytes')
    

    I have searched up many forums and not able to solve the problem. Thank you in advance!

  • Badi8beach
    Badi8beach over 10 years
    what do you mean by path input in template? I am following this example (the answer)- stackoverflow.com/questions/5871730/…
  • Badi8beach
    Badi8beach over 10 years
    isnt the url 'sent' suppose to specify where to send back the data? and where is GET being used?
  • Badi8beach
    Badi8beach over 10 years
    Ah I didn't notice the path field at all! It was before I implemented the upload feature. It all works now, thanks!
  • Badi8beach
    Badi8beach over 10 years
    I see what you were trying to get at now. I added the upload feature after adding path. Thanks tho