"CSRF token missing or incorrect."的解决方法总结

现象:
Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.
解决方法1:
django工程settings.py

'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',#确认存在
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',

解决办法2:
html中的form中添加模板标签{% csrf_token %}

{% csrf_token %}

解决办法3:django工程views.py


from django.shortcuts import render_to_response
from django.template import RequestContext
 
def some_view(request):
    # ...
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))

你可能感兴趣的:(学习)