Django输出CSRF令牌作为对象而不是值 [英] Django outputs CSRF token as object instead of value
问题描述
< input type ='hidden'name = 'csrfmiddlewaretoken'value ='{'csrf_token':django.utils.functional .__ proxy__ object at 0x1255690>}'/>
我正在使用 {%csrf_token%}
在模板中,我该如何解决这个问题? (我正在使用Django 1.2)
编辑:确切的表单代码是:
< form name =fooaction =urlmethod =POST>
{%csrf_token%}
< select>
{%的密钥,帐户在accounts.items%}
< option value ={{key}}> {{account}}< / option>
{%endfor%}
< / select>
< input type =submit>
< / form>
我发现原因:在settings.py中添加: django.middleware.csrf.CsrfViewMiddleware
但不是: django.middleware.csrf.CsrfResponseMiddleware
。
所以在 django.middleware.csrf之后添加
和 django.middleware.csrf.CsrfResponseMiddleware
CsrfViewMiddleware django.middleware.common.CommonMiddleware
,你很好。
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>
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.
这篇关于Django输出CSRF令牌作为对象而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!