1

I am struggling with the CSRF token in a simple POST form in Django. The template generates the following CSRF output instead of outputting the value of the token:

<input type='hidden' name='csrfmiddlewaretoken' value='{'csrf_token':django.utils.functional.__proxy__ object at 0x1255690>}' />

I am using {% csrf_token %} in the template, how can I fix this? (I am using Django 1.2)

EDIT: the exact form code is:

<form name="foo" action="url" method="POST">
   {% csrf_token %}

   <select>
       {% for key, account in accounts.items %} 
           <option value="{{ key }}">{{ account }}</option>
       {% endfor %}
   </select>
<input type="submit">    
</form>
2
  • Please show the exact template fragment. Commented Feb 23, 2010 at 15:01
  • Hi Daniel, I have added the form, hope this helps. When I do {{ crsf_token.csrf_token }} then the actual token is shown, however without the entire hidden input field. Commented Feb 23, 2010 at 15:26

1 Answer 1

1

I found the cause: in settings.py I had added: django.middleware.csrf.CsrfViewMiddleware but not: django.middleware.csrf.CsrfResponseMiddleware.

So add django.middleware.csrf.CsrfResponseMiddleware after django.middleware.csrf.CsrfViewMiddleware and django.middleware.common.CommonMiddleware and you are good to go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.