【问题标题】:CSRF Django, ValueErrorCSRF Django,值错误
【发布时间】:2013-12-02 12:34:06
【问题描述】:

我按照教程here 进行操作,但出现以下错误

/mapapp/ 处的 ValueError 字典更新序列元素#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

【问题讨论】:

  • 能否提供完整的堆栈跟踪和错误信息?
  • mapvis.store 是否定义了一个名为 csrf 的东西?
  • 丹尼尔,不,它没有。
  • 正如@Shes 提到的; docs 说,你的第二个参数必须是 dict of values 将被传递给模板,你的第三个参数必须是 context_instance。你传递了错误的论点。

标签: python django python-3.x django-forms django-csrf


【解决方案1】:

去掉以下几行,它们不是必需的:

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,您的视图代码应如下所示:

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)

【讨论】:

  • 这仅在您在render_to_response 调用(或render 快捷方式)中使用RequestContext 时有效,而OP 不是。
  • return render_to_response('mapvis/mapapp.html', c, {}, context_instance=RequestContext(request)) 给我“未定义全局名称'RequestContext'”
  • 不确定我应该在 'my_variable': 'thevalue'} 中包含什么,使用没有 RequestContext 的示例我从初始帖子中收到相同的错误消息。
  • 不过,您并没有复制我的示例,因为您有一个随机变量 c,您正试图将其传递给 render_to_response。摆脱它,或者把它放在 context 里面。
猜你喜欢
  • 2013-03-21
  • 2011-03-12
  • 2017-05-28
  • 2015-03-31
  • 2016-03-20
  • 1970-01-01
  • 2018-04-10
  • 2018-04-24
  • 2011-09-06
相关资源
最近更新 更多