gpt4 book ai didi

python - Django 中的 MultiValueDictKeyError

转载 作者:太空狗 更新时间:2023-10-29 22:15:59 24 4
gpt4 key购买 nike

我在执行获取请求时遇到此错误,而不是在发布请求期间。

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 "

views.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

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

ma​​in_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

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

最佳答案

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

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

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

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

另见:

关于python - Django 中的 MultiValueDictKeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23531030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com