简体   繁体   中英

django 1.11.6 csrf_token value is null?

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>

and I html view source then csrf_token value is null.

Don't use render_to_response , it's obsolete. Use render instead.

def category(request):
    dic = {
        'a': 'aaaa',
        'b': 'bbb'
    }
    return render(request, 'cate.html', dic)

context_instance parameter of render_to_response function is deprecated and was removed in Django 1.10. The problem was discussed here: Django error: render_to_response() got an unexpected keyword argument 'context_instance' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM