csrf_token未在通过post加载的页面上创建

时间:2014-03-31 23:29:46

标签: python django django-views django-csrf csrf-protection

Django中的{% csrf_token %}模板标记存在问题。在通过get加载的页面上很好但是如果我使用post加载页面,则标签不会加载到请求发布的页面上的表单。

我正在使用render_to_response来呈现网页

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

从POST请求调用render_to_response时,是否使用csrf令牌更新上下文?像这样:

from django.core.context_processors import csrf
from django.shortcuts import render_to_response

def my_view(request):
    if request.method == 'POST':
        c = {}
        c.update(csrf(request))
        return render_to_response("a_template.html", c)
    else:
        # GET code...