Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form.is_valid() returns false (django)

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!

like image 896
Badi8beach Avatar asked Dec 21 '13 21:12

Badi8beach


People also ask

Why form is valid is false in Django?

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.

How do you remove this field is required Django?

If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file.

What is clean data in Django?

TL;DR. form. cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).

What is field error in Django?

The FieldError exception is raised when there is a problem with a model field. This can happen for several reasons: A field in a model clashes with a field of the same name from an abstract base class. An infinite loop is caused by ordering.


1 Answers

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)
      
like image 50
Laxmikant Avatar answered Oct 04 '22 21:10

Laxmikant