2014-09-18 2 views
3

Django CMS, 메 자닌을 사용하여 captcha 양식의 테스트 버전을 설정하려고했습니다. 그것은 보안 문자를 표시,하지만 난 양식을 제출 때 오류 얻을 :captcha 형식의 메 자닌에서 CSRF 토큰 확인에 실패했습니다.

CSRF 검증이 실패

금지 (403). 요청이 중단되었습니다. 실패 주어진

도움말

이유 : 장고의 CSRF 메커니즘이 제대로 사용되지 않은 경우가 진정한 크로스 사이트 요청 위조, 또는 경우에 일반적으로

CSRF token missing or incorrect. 

이 발생할 수 있습니다. POST 양식의 경우 다음을 확인해야합니다.

Your browser is accepting cookies. 
The view function uses RequestContext for the template, instead of Context. 
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. 
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. 

You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. 
You can customize this page using the CSRF_FAILURE_VIEW setting. 

동작은 Firefox 및 Chrome (시크릿 여부 또는 상관 없음)과 동일합니다. Python 3.4, Django 1.6.7, Mezzanine 3.1.0을 사용하고 있습니다. 내 captcha_test.views.py에서

TEMPLATE_CONTEXT_PROCESSORS = (
    ... 
    "django.core.context_processors.csrf", 
) 
MIDDLEWARE_CLASSES = (
    ... 
    "django.middleware.csrf.CsrfViewMiddleware", 
) 

3) : 내 settings.py 파일에서

<body> 
    <h3>Captcha</h3> 
    <form method="POST"> 
     {% csrf_token %} 
     <input name="item_text" id="id_new_item" placeholder="Enter item"> 
     <br> 
     {{ form.captcha }} 
     <input type="submit" value="Submit"> 
    </form> 
</body> 

2) : 1) 내 HTML 템플릿 : 나는 여러 가지 방법으로 문제를 해결하기 위해 노력했다 :

from django.views.decorators.csrf import csrf_protect 
from django.shortcuts import render_to_response 
from django.http import HttpResponse 

from captcha_test.forms import CaptchaTestForm 

@csrf_protect 
def captcha_page(request): 
    if request.POST: 
     form = CaptchaTestForm(request.post) 
     if form.is_valid(): 
      human = True 
      return HttpResponseRedirect('/') 
    else: 
     form = CaptchaTestForm() 
    return render_to_response('captcha.html', locals()) 

내 forms.py 파일이 전혀 도움이된다면 :

from django import forms 
from captcha.fields import CaptchaField 

class CaptchaTestForm(forms.Form): 
    item_text = forms.CharField() 
    captcha = CaptchaField() 

통찰력이 있으십니까? 당신의 도움을 주셔서 감사합니다!

By default, the template will be rendered with a Context instance (filled with values from dictionary). If you need to use context processors, render the template with a RequestContext instance instead.

그래서 context_instance=RequestContext(request)을 추가하는 것은 해결해야 할 문제 : documentationrender_to_response까지,

return render_to_response('captcha.html', locals()) 

을 그리고 :

답변

2

을 확인해야합니다

The view function uses RequestContext for the template, instead of Context .

하지만 당신은 사용할 수 있습니다.

관련 문제