(Django)上载文件总是失败

2021-04-11 15:48:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Django将文件上载到服务器,但是form.is_valid总是返回Flase。然后输出如下所示的表单值。在

打印表单视图.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>

在表单.py在

^{pr2}$

在网址.py在

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

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

在视图.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})

在主页.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条回答
网友
1楼 ·

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

if form.is_valid():

    temp = form.save(commit=False)
    country = form.cleaned_data['country']
    email = form.cleaned_data['email']
    temp.save()
    return HttpResponse('/thanks/')
网友
2楼 ·

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

因为,很明显,在视图.py你分享的)

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

相关问题