"CSRF token missing or incorrect."해결책

1346 단어 django
1、setting.py:
MIDDLEWARE_CLASSES = (
    '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、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))

CSRF를 차단하려면
방법1:django공정settings를 주석해 버리세요.py중
#'django.middleware.csrf.CsrfViewMiddleware'

방법2:django공정views.py 차폐 장식기 추가
from django.views.decorators.csrf import csrf_exempt 
@csrf_exempt
def some_view(request):
    #...

전재:http://www.cnblogs.com/zhujiabin/p/8260288.html

좋은 웹페이지 즐겨찾기