Django ModelForm不验证图像文件上传

时间:2015-11-28 02:40:12

标签: django forms file-upload

我有一个带有一个图像字段的ModelForm,如下所示:

class ProfilePhotoForm(ModelForm):
    class Meta:
        model = ProfilePhoto
        fields = ["image"]

我有一个使用ModelForm的View,如下所示:

class ProfileView(View):

    def get(self, request):
        user = User.objects.first()
        profile = Profile.objects.get(user=user)
        photo_form = ProfilePhotoForm(instance=profile)
        profile_form = ProfileForm(instance=profile)
        return render(request, "edit_profile.html", {"photo_form": photo_form, "profile_form": profile_form })

    def post(self, request):    
        if 'picture' in self.request.POST:
            photo_form = ProfilePhotoForm(request.POST, request.FILES, prefix='photo_form')
            import ipdb; ipdb.set_trace()
            if photo_form.is_valid():
                image = photo_form.cleaned_data['image']
                user = User.objects.first()
                profile = Profile.objects.get(user=user)
                profile_photo = ProfilePhoto.objects.get(profile=profile)
                if profile_photo:
                    profile_photo.delete()
                ProfilePhoto.objects.create(profile=profile, image=image)
        elif 'profile' in self.request.POST:
            profile_form = ProfileForm(request.POST, prefix='profile_form')
            if profile_form.is_valid():
                profile_form.save()
        return render(request, "edit_profile.html", {})

使用视图的GET请求呈现的模板如下:

<div class="wrapper">   
    <form method="POST" enctype="multipart/form-data" action="">
        <input type="hidden" name="csrfmiddlewaretoken" value="5sY3rFOqYjJTAkOUKVcoAkIthA10RbCc">
        <input class="form-control margin10" id="id_image" name="image" placeholder="Image" type="file">
        <input class="btn btn-default btn-block btn-primary margin10" id="register" type="submit" name="picture" value="Upload Profile Picture">
    </form>
    <form method="POST" action="">
        <input type="hidden" name="csrfmiddlewaretoken" value="5sY3rFOqYjJTAkOUKVcoAkIthA10RbCc">
        <input class="form-control margin10" id="id_display_name" maxlength="50" name="display_name" placeholder="Display name" type="text" value="Kent Shikama">
        <textarea class="form-control margin10" cols="40" id="id_description" name="description" placeholder="Description" rows="10">A CS major taking CS133</textarea>
        <input class="btn btn-default btn-block btn-primary margin10" id="register" type="submit" name="profile" value="Change Profile">
    </form>
</div>

在ProfileView类中,我放置了一个ipdb调试停止。以下是一些可能相关的输出:

ipdb> photo_form.data
<QueryDict: {'picture': ['Upload Profile Picture'], 'csrfmiddlewaretoken': ['5sY3rFOqYjJTAkOUKVdoAkIthA10RbCc']}>
ipdb> photo_form.files
<MultiValueDict: {'image': [<InMemoryUploadedFile: self_square.jpg (image/jpeg)>]}>
ipdb> photo_form.errors
{'image': ['This field is required.']}

我已经搜索了很多相关的问题:他们中的大多数都说要仔细检查该请求.FILES已经传递到表单并且设置了enctype。 photo_form.files返回InMemoryUploadedFile的事实似乎表明图像确实已上传。因此,我很困惑为什么表单没有验证。任何人都有任何想法?

1 个答案:

答案 0 :(得分:0)

问题在于名称与前缀'photo_form-image'的组合与'image'的HTML输入名称不匹配

我在逐步通过ipdb时发现了这个:

45.64.64.0   / 255.255.252.0
103.28.248.0 / 255.255.252.0
149.126.72.0 / 255.255.248.0
185.11.124.0 / 255.255.252.0
192.230.64.0 / 255.255.192.0
198.143.32.0 / 255.255.224.0
199.83.128.0 / 255.255.248.0