Python Django 中的 MultiValueDictKeyError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23531030/
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 03:05:33  来源:igfitidea点击:

MultiValueDictKeyError in Django

pythondjango

提问by user1050619

I get this Errorwhen I do a get request and not during post requests.

我在执行 get 请求时而不是在 post 请求期间收到此错误

Error:-

MultiValueDictKeyError at /StartPage/

"Key 'username' not found in <QueryDict: {}>"

Request Method: GET Request URL:

http://127.0.0.1:8000/StartPage/

Django Version: 1.4 Exception Type: MultiValueDictKeyError Exception Value:

"Key 'username' not found in "

错误:-

MultiValueDictKeyError 在 /StartPage/

"Key 'username' not found in <QueryDict: {}>"

请求方式:GET 请求地址:

http://127.0.0.1:8000/StartPage/

Django 版本:1.4 异常类型:MultiValueDictKeyError 异常值:

“在”中找不到关键的“用户名”

views.py

视图.py

from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.template import Context, RequestContext
@csrf_exempt
def main_page(request):
    return render_to_response('main_page.html')

@csrf_exempt
def Start_Page(request):
    if request.method == 'POST':
       print 'post', request.POST['username']
    else:
       print 'get', request.GET['username']
    variables = RequestContext(request,{'username':request.POST['username'],
           'password':request.POST['password']})
    return render_to_response('Start_Page.html',variables)

urls.py

网址.py

from polls.views import *
urlpatterns = patterns('',
    # Examples:
     url(r'^$', main_page),
     url(r'^StartPage/$', Start_Page)

main_page.html

main_page.html

<html>
<head>
</head>
<body>
This is the body
<form method="post" action="/StartPage/">{% csrf_token %}
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Sign with password">
</form>
</body>
</html>

Start_Page.html

Start_Page.html

<html>
<head>
</head>
<body>
This is the StartPage
Entered user name ==   {{username}}
Entered password  == {{password}}
</body>
</html>

采纳答案by alecxe

Sure, you are not passing usernameas a GETparameter while getting the http://127.0.0.1:8000/StartPage/page.

当然,您在获取页面时不会username作为GET参数传递http://127.0.0.1:8000/StartPage/

Try this and observe username printed: http://127.0.0.1:8000/StartPage?username=test.

试试这个并观察用户名打印:http://127.0.0.1:8000/StartPage?username=test

Use get()and avoid MultiValueDictKeyErrorerrors:

使用get()和避免MultiValueDictKeyError错误:

request.GET.get('username', '') 

See also:

也可以看看:

回答by Shehroz Ayaz

Make sure the request you are getting doesn't contains disabled. If the field which you are getting contains disabled. It will give this error. So, in order to solve this make sure your don't have a disabledwork in your field. e.g

确保您收到的请求不包含disabled。如果您获取的字段包含disabled。它会给出这个错误。因此,为了解决这个问题,请确保您的领域中没有残疾人工作。例如

 <input  name="numberid" disabled class="form-control"  value="{{p.id}}" type="text"></div>

In my case, the disabledkeyword was causing this error.

就我而言,disabled关键字导致了此错误。