Django表单不会验证

时间:2013-12-24 06:06:58

标签: python html django forms validation

我正在使用Django表单,出于某种原因,此表单不会验证!它提交正常,或者至少runserver显示一个带有代码200的ok post响应(ok)。出于某种原因,我的表单不会通过is_valid测试!

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)
    )

这是我的模板代码段:

    <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>

这是我的表格类:

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

请帮助我!

3 个答案:

答案 0 :(得分:1)

如果new_Show是模型,为什么不创建ModelForm而不是forms.Form

所以,而不是

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

为什么不使用,

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

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

使用ModelForm将确保表单验证符合模型验证。此外,它可以切断你的代码(特别是第6到11行)。

答案 1 :(得分:0)

如果is_valid()看到它给出的错误,将有助于将这两行添加到您的视图中:

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():

您可以在此处粘贴错误,我们可以查看问题

答案 2 :(得分:-1)

由于您已将2个Django表单放在一个HTML表单标记下,因此当您在前端提交表单时,您通过request.POST发送了一个额外的字段,表明您的NewShowForm没有有。如果将两个表单合并为一个Django表单,您应该可以使其工作。