Я пытался настроить тестовую версию формы капчи, используя Django CMS, Mezzanine. Он показывает капчу, но когда я отправляю форму, я получаю сообщение об ошибке:
Запрещено (403)
Проверка CSRF не удалась. Запрос прерван.
Помощь
Указана причина отказа:
CSRF token missing or incorrect.
Как правило, это может произойти, когда имеет место настоящая подделка межсайтовых запросов или когда механизм Django CSRF используется неправильно. Для 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. Я пытался решить проблему несколькими способами: 1) Мой html-шаблон:
<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) В моем файле settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.csrf",
)
MIDDLEWARE_CLASSES = (
...
"django.middleware.csrf.CsrfViewMiddleware",
)
3) В моем captcha_test.views.py:
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()
Любые идеи? Спасибо за вашу помощь!