Django outputs CSRF token as object instead of value

好久不见. 提交于 2019-12-11 05:23:01

问题


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>

回答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.



来源:https://stackoverflow.com/questions/2318919/django-outputs-csrf-token-as-object-instead-of-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!