【问题标题】:form.is_valid() returns false (django)form.is_valid() 返回 false (django)
【发布时间】:2014-01-10 11:56:42
【问题描述】:

我对 django 有点陌生。我试图在上传中选择文件后将文件发送到另一台服务器,但是form.is_valid() 总是返回 false 不会让我输入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模板-

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

我搜索了很多论坛都无法解决问题。 提前谢谢!

【问题讨论】:

  • 您可以添加您的forms.py 文件内容吗?
  • 您的表单类应使用CamelCase 命名,例如SendFileForm.
  • 是的,我会添加我的forms.py
  • 它现在应该匹配表单命名约定。但是对于为什么表单没有被验证有任何提示吗?

标签: html django django-forms django-templates django-views


【解决方案1】:

默认情况下,表单中的每个字段都是必需的 (required=True)。提交的表格没有在必填字段中提供信息是无效的。您可以在模板中将path 字段添加到您的表单中,并且必须填写,或者您可以使路径不需要:

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

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

【讨论】:

  • 啊,我根本没有注意到path 字段!那是在我实现上传功能之前。现在一切正常,谢谢!
【解决方案2】:

注意:-此答案仅用于帮助您调试代码。

您可以直接在视图中打印表单错误。

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)
      

【讨论】:

    【解决方案3】:

    问题是您的模板中没有path 输入。您的 request.POST 包含不完整的数据,这就是您的表单无效的原因。

    您的模板中缺少此内容:

    {{ form.path }}
    

    【讨论】:

    • 在模板中输入path是什么意思?我正在关注这个例子(答案)-stackoverflow.com/questions/5871730/…
    • url 'sent' 不应该指定将数据发回的位置吗? GET 在哪里使用?
    • 我明白你现在想要得到什么。我在添加path 后添加了上传功能。谢谢你
    猜你喜欢
    • 2020-04-06
    • 2020-05-09
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2021-11-08
    • 2018-12-17
    相关资源
    最近更新 更多