Невозможно импортировать файл и текст из одной HTML-формы - PullRequest
0 голосов
/ 17 мая 2019

Мне нужно импортировать файл и текст (поле) из той же HTML-формы. Но я получаю ошибку:

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))  

Error

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.

1 Ответ

0 голосов
/ 17 мая 2019

Это не имеет ничего общего с загрузкой файла.Как ясно показывает ошибка, это происходит в поле name;потому что вы пытаетесь получить форму request.GET вместо request.POST.

Действительно, вы должны использовать форму Django.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...