我有一个html表单,用户可以上传文件,但是如果用户直接单击提交按钮而不上传任何文件,django就会出错。我该如何控制这个问题?
<!-- Upload form. Note enctype attribute! -->
<form method="post" enctype="multipart/form-data" multiple>
{% csrf_token %}
<input type= "file" name ="docfile" multiple/>
<button id="yukle" type="submit" value="Upload">
<img src="/static/img/upload.png" width=28 height=30 alt="Yükle"/>
</button>
</form>
def SenetList(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
dosya_tipi = "Senet"
context = upload(request.FILES.getlist('docfile'),form, dosya_tipi)
return render(request, 'operations/upload.html', context)
else:
form = DocumentForm()
return render(request, 'operations/upload.html', {'form': form})
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
我给出了这样的错误:
/ upload / senet处的ValueError
视图operation.views.SenetList没有返回HttpResponse对象。它返回None。
答案 0 :(得分:0)
如果方法为post且表单无效,则将隐式返回None。这会导致错误。为表单无效的方法发布实施回复