我有一个代码,代码运行良好,这个代码的问题是上传文件时它没有获取文件名。
视图.py
@csrf_exempt
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
if 'filename' in request.FILES:
filename = request.FILES['filename']
else:
return HttpResponseRedirect('/user_profileform/')
else:
form = UploadFileForm()
return render_to_response('user_profile.html', {'form': form })
def handle_uploaded_file(f):
destination = open('media/name', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
我的表格是:-
<form action="/user_profileform/" method="POST" enctype="multipart/form-data" name="uform" id="userform">{% csrf_token %}
{{form}}
<input type="submit" value="submit" name="usubmit">
</form>
使用此文件时,文件已正确上传,但未获取文件名,为什么。