上传图片时,Django form.is_valid()始终为False

时间:2016-12-26 15:06:43

标签: django django-models django-forms django-views

views.py

我正在尝试上传图片,但form.is_valid总是返回false

def  upload(request):

    if request.method=="POST":

        form=uploadform(request.POST,request.FILES)
        if form.is_valid():
            cd=cleaned_data
            form.save(commit=True)
            return render(request,"success.html")
        else:
            form=uploadform()
            return render(request,'pico.html',{'form':form})

这是表格

<form action="" method="post"> 
         {{form.as_p}}
         {% csrf_token %}
<input type="submit" value="Submit" />
</form>

forms.py

class uploadform(ModelForm):
    class Meta:
        model=examplemodel
        fields=['pic']

models.py

class examplemodel(models.Model):
    pic=models.ImageField()

1 个答案:

答案 0 :(得分:0)

您是否在html表单标签中设置了enctype="multipart/form-data"