我有以下表格(以及其他两种表格):
class UploadFileForm(forms.Form):
file = forms.FileField()
以下是我的HTML:
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
<form action="" method="POST" role="form" enctype="multipart/form-data">
{% csrf_token %}
<legend>Upload a file: </legend>
<div class="form-group">
<input type="file" name="file" class="form-control" id="" placeholder="Input field">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
完成文件处理:
if request.method == 'POST':
print "I am in request."
print request.FILES['file']
# create a form instance and populate it with data from the request:
formName = NameForm(request.POST)
formHandle = HandleForm(request.POST)
fileForm = UploadFileForm(request.POST)
successMessages = []
failureMessages = []
# check whether it's valid:
#
if fileForm.is_valid():
print "file is uploaded."
print request.FILES['file']
else:
print "File form is not set."
即使设置了request.files
中的值,也会执行其他条件。我做错了什么?
答案 0 :(得分:3)
您需要添加request.FILES
,如下所示:
fileForm = UploadFileForm(request.POST, request.FILES)