我需要从相同的html格式导入文件和text(field)。但是我得到了错误:
Unable to upload file. MultiValueDictKeyError('name',)
new.html
<form action="{% url "upload2" %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="text" name="name">
<input type="file" name="xlfile">
<button type="submit">Upload</button>
</form>
veiw.py
def upload2(request):
if "GET" == request.method:
return render(request, "homepage/new.html")
try:
myfile = request.FILES['xlfile']
cname = request.GET['name']
print(cname)
return HttpResponse('ok')
except Exception as e:
logging.getLogger("error_logger").error("Unable to upload file. "+repr(e))
messages.error(request,"Unable to upload file. "+repr(e))
错误
Unable to upload file. MultiValueDictKeyError('name',)
Internal Server Error: /upload2
Traceback (most recent call last):
File "/home/csdj/django/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/csdj/django/django/core/handlers/base.py", line 126, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view homepage.views.upload2 didn't return an HttpResponse object. It returned None instead.
答案 0 :(得分:0)
这与上传文件没有任何关系。正如错误清楚表明的那样,它正在name
字段上发生;因为您正尝试从request.GET
而不是request.POST
的形式获取它。
但是,实际上,您应该使用Django表单。