gpt4 book ai didi

python - CSRF Django,值错误

转载 作者:行者123 更新时间:2023-12-01 05:30:55 25 4
gpt4 key购买 nike

我按照教程here进行操作但我收到以下错误

/mapapp/处的值错误字典更新序列元素#0的长度为1;需要2个

这些都是包含csrf相关代码的文件

views.py

from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from django.template import Template, Context
from django.http import HttpResponse
from mapvis.store import *
import datetime

def mapapp(request):
csrfprotection = {}
csrfprotection.update(csrf(request))

....

return render_to_response('mapvis/mapapp.html', c, csrfprotection)

settings.py

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ma​​ppapp.html

<body>
<div id="map_canvas"></div>
<form method="post">{% csrf_token %}
<strong>Start</strong><br>
Lng: <input type="text" id="start_lng"><br>
Lat: <input type="text" id="start_lat"><br>
<strong>Destination</strong><br>
Lng: <input type="text" id="dest_lng"><br>
Lat: <input type="text" id="dest_lat"><br>
<input type="submit" style="background-color:#64FE2E" type="button" id="go" value="go">
</form>
</body>

:编辑

Internal Server Error: /mapapp/
Traceback (most recent call last):
File "/sw/django-1.5.4/lib/python3.2/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/piehe154/maps/lmap/mapvis/views.py", line 28, in mapapp
return render_to_response('mapvis/mapapp.html', c, csrfprotection)
File "/sw/django-1.5.4/lib/python3.2/site-packages/django/shortcuts/__init__.py", line 29, in render_to_response
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/sw/django-1.5.4/lib/python3.2/site-packages/django/template/loader.py", line 175, in render_to_string
context_instance.update(dictionary)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

最佳答案

删除以下几行,它们不是必需的:

csrfprotection = {}
csrfprotection.update(csrf(request))

但请确保在渲染到模板时使用 RequestContext:

from django.template import RequestContext

def mapapp(request):
# context contains key/value pairs used in your template
c = "GOOGLE_API_KEY"
context = { 'myvariable': 'thevalue', 'c': c }
context.update(csrf(request))
return render_to_response('mapvis/mapapp.html', context, context_instance=RequestContext(request))

如果您不想使用 RequestContext,您的 View 代码应如下所示:

def mapapp(request):
# context contains key/value pairs used in your template
c = "GOOGLE_API_KEY"
context = { 'myvariable': 'thevalue', 'c': c }
context.update(csrf(request))
return render_to_response('mapvis/mapapp.html', context)

关于python - CSRF Django,值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20328494/

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