当我向FileUploadForm提交一个空文件时,它无效,我返回以下响应:
return render_to_response('upload.html', {'form': form})
然而,此响应会导致一种奇怪的行为,因为模板内容不会呈现给HTML页面,而是会附加到URL并产生以下请求:
请求网址:127.0.0.1:8000/%3C?xml%20version=%221.0%22%20?%3E%3C!DOCTYPE%20html%20PUBLIC%20%22-//W3C//DTD%20XHTM [ ....]
views.py
def upload_file(request):
if request.method == 'POST':
form = SampleUploadForm(request.POST, request.FILES)
if form.is_valid():
...
else:
form = SampleUploadForm()
return render_to_response('upload.html', {'form': form})
upload.html
<form enctype="multipart/form-data" method="POST" action="{% url 'upload_file' %}" id="file_form">
<input type="button" id="pbutton" value="Select file.." name="pbutton">
<input type="file" name="file" id="button_file" />
<div id="label_wrapper">
<label id="file_label" >No File selected...</label>
</div>
</form>
已经验证过TEMPLATE_CONTEXT_PROCESSORS,TEMPLATE_LOADERS&amp; MIDDLEWARE_CLASSES的正确性。