form.is_valid() returns false (django)
我对姜戈有点陌生。我试图在上传时将文件发送到另一个服务器,但是如果
埃多克斯1〔2〕-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | {% 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> |
号
埃多克斯1〔4〕-
1 2 3 | class SendFileForm(forms.Form): path = forms.CharField() songfile = forms.FileField(label='Select a music file',help_text='max. 4 megabytes') |
我搜了很多论坛,没能解决这个问题。提前谢谢!
默认情况下,表单中的每个字段都是必需的(
1 2 3 | class SendFileForm(forms.Form): path = forms.CharField(required=False) ... |
号
或
1 2 3 4 5 6 | <form action={% url"sent" %} method="post" enctype="multipart/form-data"> ... {{ form.songfile }} {{ form.path }} ... </form> |
问题是模板中没有
模板中缺少此项:
1 | {{ form.path }} |