即使在上传文件后,此字段也是必填错误

时间:2015-07-14 10:24:33

标签: django

myview.py
def index_Expense(request):
    data=Expense.objects.all()
    form=ExpenseForm()
    if request.method == 'POST':
        form = ExpenseForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/Expense/')
    return render_to_response('expense.html',locals(),context_instance=RequestContext(request))

mymodel.py

class Expense(models.Model):
    Expense_No=models.AutoField(primary_key=True)
    Expense_Date=models.DateField(null=False)
    Amount=models.FloatField(null=False)
    Receipt=models.ImageField(upload_to='pic/',blank=False)  

使用上面的视图我试图使用表单添加记录。当我点击添加按钮时,即使我上传文件,我也会看到收据字段表单上的字段是必填错误。

1 个答案:

答案 0 :(得分:1)

确保您已在模板中为表单设置enctype

<form enctype="multipart/form-data" method="post" action="/foo/">

有关详细信息,请参阅docs on binding uploaded files to a form