(Django)上传文件总是失败

时间:2017-05-18 03:02:41

标签: python html django html5

我尝试使用Django将文件上传到服务器,但form.is_valid始终返回Flase。然后我输出了如下所示的表格值。

在views.py中打印表单

<tr><th><label for="id_country">country:</label></th><td><input type="text" name="country" value="DE" required id="id_country" maxlength="200" /></td></tr>
    <tr><th><label for="id_email">email:</label></th><td><input type="email" name="email" value="123456789@example.com" required id="id_email" /></td></tr>
    <tr><th><label for="id_filetoupload">fileToUpload:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="file" name="filetoupload" required id="id_filetoupload" /></td>
    </tr>

form.py

from django import forms


class UserInfo(forms.Form):
    country = forms.CharField(label='country', max_length=200)
    email = forms.EmailField(label='email')
    filetoupload = forms.FileField(label='fileToUpload')

urls.py

from django.conf.urls import url
from loadfileweb.views import receiveinfo

urlpatterns = [
    url(r'^home/', receiveinfo),
    url(r'^home/submit', receiveinfo)
]

views.py

    def receiveinfo(request):
        if request.method == 'POST':
            form = UserInfo(request.POST, request.FILES)
            print "form info"
            print form
            print form.is_valid()
            if form.is_valid():
                # process the data in form.cleaned_data as required
                country = form.cleaned_data['country']
                email = form.cleaned_data['email']
                print email
                print "------------ get email -----------"



                # redirect to a new URL:
                return HttpResponse('/thanks/')
            else:
                print "-----   form isn't valid   -------"

                return render(request, 'home.html', {'form': form})
        else:
            print "--------- request.method != 'POST'------------"
            form = UserInfo()
        return render(request, 'home.html', {'form': form})

home.html的

<form class='UserInfo' action="submit" method="post" enctype="multipart/form-data">{% csrf_token %}
                    E-mail: <input type="email" name="email" autofocus><br>
                    {{ form.email.errors}}<br>
                    Country: <input list="countrycode" name="country">
                    {{ form.country.errors}}<br>
                    <datalist id="countrycode">
                        <option value="DE">
                        <option value="ES">
                        <option value="FR">
                        <option value="IT">
                        <option value="UK">
                        <option value="JP">
                        <option value="CA">
                        <option value="USA">
                    </datalist>
                    <br>
                    Select a file to upload:
                    <input type="file" name="fileToUpload" id="fileToUpload" ><br>
                    {{ form.filetoupload.errors}}<br>
                    <input type="submit"><br>
                </form>

我在谷歌和许多论坛上搜索过但仍有问题。希望对你有所帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

1.您尚未保存表格。 你可以试试:

if form.is_valid():

    temp = form.save(commit=False)
    country = form.cleaned_data['country']
    email = form.cleaned_data['email']
    temp.save()
    return HttpResponse('/thanks/')

答案 1 :(得分:0)

1)使文件字段可选 - 您必须将表单中的filetoupload字段设置为required=False非强制性,或者在提交之前上传文件!

因为很明显,您已共享的views.py中的This field is required.中包含print forms

2)要使您的文件字段成为必填项,您需要修改表单中的filetoupload以匹配html元素的name属性,反之亦然!

class UserInfo(forms.Form):
    country = forms.CharField(label='country', max_length=200)
    email = forms.EmailField(label='email')
    filetoUpload = forms.FileField(label='fileToUpload')