Python MultiValueDictKeyError / request.POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30696030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:50:19  来源:igfitidea点击:

MultiValueDictKeyError / request.POST

pythondjango

提问by Kwang

I think I hav a problem at request.POST['title']

我想我在request.POST['title']有问题

MultiValueDictKeyError at /blog/add/post/ "'title'" Request Method: GET Request URL: http://119.81.247.69:8000/blog/add/post/Django Version: 1.8.2 Exception Type: MultiValueDictKeyError Exception Value:
"'title'" Exception Location: /usr/local/lib/python2.7/dist- packages/django/utils/datastructures.py in getitem, line 322 Python Executable: /usr/bin/python Python Version: 2.7.3

MultiValueDictKeyError at /blog/add/post/ "'title'" 请求方法:GET 请求 URL:http://119.81.247.69:8000 /blog/add/post/ Django 版本:1.8.2 异常类型:MultiValueDictKeyError 异常值:
“'title'”异常位置:/usr/local/lib/python2.7/dist-packages/django/utils/datastructures.py in getitem,第 322 行 Python 可执行文件:/usr/bin/python Python 版本:2.7.3

views.py

视图.py

def add_post(request):
    entry_title = request.POST["title"]
    return HttpResponse('Hello %s' % entry_title)

write.html

写.html

<form method="POST" action="/blog/add/post/">
<p>
    <label for "title">Title</label>
    <input type="text" id="title" name="title" value="" />
</p>
<p>
    <label for 'category'>Category</label>
    <select id="category" name="category"></select>
</p>
<p>
    <label for 'tags'>Tags</label>
    <input type="text" id="tags" value="" />
</p>
<p>
    <textarea id="content" name="content"></textarea>
</p>
<p>
    <input type="submit" value="Write" />
</p>

采纳答案by Brandon

Change:

改变:

def add_post(request):
    entry_title = request.POST["title"]
    return HttpResponse('Hello %s' % entry_title)

to:

到:

def add_post(request):
    entry_title = request.POST.get("title", "Guest (or whatever)")
    return HttpResponse('Hello %s' % entry_title)

and it won't throw a KeyError, but you should look at using Django's forms rather than pulling values directly from the POST data.

它不会抛出KeyError,但您应该考虑使用 Django 的表单,而不是直接从 POST 数据中提取值。

Alternatively, you can keep your existing code and simply check for the exception:

或者,您可以保留现有代码并简单地检查异常:

def add_post(request):
    try:
        entry_title = request.POST["title"]
    except KeyError:
        entry_title = "Guest"
    return HttpResponse('Hello %s' % entry_title)

but this is what .get()does internally already.

但这.get()已经是内部所做的了。

回答by alTus

As your traceback says: Request Method: GET. So your POST dict is obviously empty and thus you get your KeyError.

当你的回溯说:Request Method: GET。所以你的 POST dict 显然是空的,因此你得到了你的KeyError.

回答by lilhamad

I had the same problem, I discovered that I forgot to add "name=" text" "in my input typein my Html page..

我有同样的问题,我发现我忘了加"name=" text" "在我input type在我的HTML页面..

回答by chpawankr

In Django project, I was facing the same problem, I did a mistake in url.py

在 Django 项目中,我遇到了同样的问题,我在 url.py 中犯了一个错误

wrong

错误的

path('support/',views.**support**,name='support'),

path('verifyDB/',views.**support**,name='verifyDB'),

correct one

正确的

path('support/',views.**support**,name='support'),

path('verifyDB/',views.**verifyDB**,name='verifyDB'),

so, check your path in view.py maybe there is a mistake.

因此,请检查您在 view.py 中的路径,也许有错误。

回答by Smriti Agrawal

For accessing Files in POST method this could be because you might have missed encrypting the files in form tag of the HTML file i.e.-

对于在 POST 方法中访问文件,这可能是因为您可能错过了对 HTML 文件的表单标记中的文件进行加密,即-

{form action="upload" method="POST" enctype="multipart/form-data"}
                                    ^^^^^^^^

is required to avoid MultiValueDictError.

需要避免 MultiValueDictError。