Search code examples
pythondjangopython-3.xdjango-formsdjango-csrf

CSRF Django, ValueError


I follow the tutorial here but i get the following error

ValueError at /mapapp/ dictionary update sequence element #0 has length 1; 2 is required

These are all the files which has csrf related code in them

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',
)

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

:EDIT

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

Solution

  • Get rid of the following lines, they aren't needed:

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

    But make sure you use RequestContext when rendering to a template:

    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))
    

    If you don't want to use RequestContext though, your view code should look like:

    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)