django 1.11.6 csrf_token值是否为空?

时间:2017-10-23 21:31:14

标签: python django csrf

settings.py

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

myview.py

@csrf_protect #ensure_csrf_cookie....etc i tried it.
def category(request):
    context = RequestContext(request)
    dic = {
        'a': 'aaaa',
        'b': 'bbb'
    }
    return render_to_response('cate.html', dic, context_instance=context)

cate.html

<form name="cateForm" id="cateForm" method="POST">
    {% csrf_token %}
    <input type="text" name="href" id="href" size="50">
</form>

我html查看源,然后csrf_token值为null。

2 个答案:

答案 0 :(得分:2)

不要使用PE32 executable (CONSOLE),它已经过时了。请改用render_to_response

render

答案 1 :(得分:0)

不推荐使用

render_to_response函数的context_instance参数,并在Django 1.10中删除了该参数。这里讨论了这个问题:Django error: render_to_response() got an unexpected keyword argument 'context_instance'