Matthew Darnell
Matthew Darnell

Reputation: 1365

Django form will not validate

I am working with Django forms and for some reason, this form will not validate! It submits alright, or at least the runserver shows an http post response with code 200 (ok). For some reason though, my form will not pass the is_valid test!

views.py:

def new_show(request):
if request.method == 'POST':
    img_form = ImageForm(request.POST, request.FILES)
show_form = NewShowForm(request.POST)
if show_form.is_valid():
    new_Show = Show()
    new_Show.title=show_form.cleaned_data['title']
    new_Show.body=show_form.cleaned_data['body']
    new_Show.pub_date=timezone.now()
    new_Show.location=show_form.cleaned_data['location']
    new_Show.time=show_form.cleaned_data['time']
    new_Show.save()     
    if img_form.is_valid():
        image=Image(image=request.FILES['imageFile'])
        new_Show.image_set.add(image)
        return HttpResponseRedirect(reverse('shows'))

    else:
        return HttpResponseRedirect(reverse('shows'))
else:
    show_form = NewShowForm()
    img_form = ImageForm()
return render_to_response(
    'shows/new_show.html',
    {'show_form': show_form, 'img_form': img_form},
    context_instance=RequestContext(request)
    )

Here is my template snippet:

    <form action="{% url "new_show" %}" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <p>{{ show_form.non_field_errors }}</p>
    <p>
        <label for="title">Title:</label>
        <input type="text" name="title"/>
    </p>
    <p>
        <label for="body">Body:</label>
        <textarea type="text" name="body"> </textarea>
    </p>

    <p>
        <label for="location">Location:</label>
        <input type="text" name="location"/>
    </p>
    <p>
        <label for="time">Date:</label>
        <input type="text" id="time" maxlength="25" size="25" name="time"><a href="javascript:NewCal('time','ddmmmyyyy',true,24)"><img src="{{ STATIC_URL }}../../static/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>

    </p>



<!-- Upload Form. Note enctype attribute! -->


    {% csrf_token %}
    <p>{{ img_form.non_field_errors }}</p>
    <p>{{ img_form.imageFile.label_tag }}</p>
    <p>
        {{ img_form.imageFile.errors }}
        {{ img_form.imageFile }}
    </p>

<p><input type="submit" value="Add Upcoming Show"></input></p>
</form>

Here is my form Class:

class NewShowForm(forms.Form):
    title=forms.CharField()
    body=forms.CharField(widget=forms.TextArea)
    location=forms.CharField()
    time=forms.DateTimeField(required=True)
class ImageForm(forms.Form):
    imageFile = forms.FileField(required=False, label='Select an Image')

Please help me!

Upvotes: 1

Views: 1734

Answers (3)

morinx
morinx

Reputation: 635

It will help to add these two lines to your view before if is_valid() to see the errors it's giving:

if request.method == 'POST':
  img_form = ImageForm(request.POST, request.FILES)
  show_form = NewShowForm(request.POST)

  print(form.is_valid())
  print(form.errors)

  if show_form.is_valid():

You can paste the errors here and we can see what's the issue

Upvotes: 1

adityasdarma1
adityasdarma1

Reputation: 325

If new_Show is a model, why not create a ModelForm instead of forms.Form?

So, instead of

class NewShowForm(forms.Form):
    title=forms.CharField()
    body=forms.CharField(widget=forms.TextArea)
    location=forms.CharField()
    time=forms.DateTimeField(required=True)
class ImageForm(forms.Form):
    imageFile = forms.FileField(required=False, label='Select an Image')

why not using,

from django.forms import ModelForm
class NewShowForm(ModelForm):
    class Meta:
        model = NewShow

class ImageForm(ModelForm):
    class Meta:
        model = Image

?

Using ModelForm will ensure that form validation meets that of model. Moreover, it can cut off your code (especially line 6 to 11).

Upvotes: 1

xjtian
xjtian

Reputation: 1006

Since you've put 2 Django forms together under one HTML form tag, when you submit the form on the front-end you're sending an extra field through request.POST that your NewShowForm doesn't have. If you combine both forms into a single Django form, you should be able to get this to work.

Upvotes: -1

Related Questions