我正在尝试基于Django Classifieds App创建一个 Django 应用程序,但在尝试提交表单时出现错误:CSRF verification failed. Request aborted. Reason given for failure: CSRF token missing or incorrect.
我确实有一个{% csrf_token %}
形式:
<form method="post" action="{% url classifieds.views.create.checkout ad.pk %}">
{% csrf_token %}
<table>
{{ form }}
<tr>
<th><label>Total:</label></th>
<td><div id="total">Choose options above</div></td>
</tr>
</table>
</form>
我也在使用from django.template import RequestContext
我还在'django.middleware.csrf.CsrfViewMiddleware',
我的 settings.py 中包含了 MIDDLEWARE_CLASSES
为了正确提交表格,我还缺少什么?
该函数如下所示:
def checkout(request, adId):
ad = get_object_or_404(Ad, pk=adId)
if request.method == 'POST':
form = CheckoutForm(request.POST)
if form.is_valid():
...
payment.save()
if django_settings.DEBUG:
paypal_form = PayPalPaymentsForm(initial=paypal_values).sandbox()
else:
paypal_form = PayPalPaymentsForm(initial=paypal_values).render()
return render_to_response('classifieds/paypal.html', {'form': paypal_form}, context_instance=RequestContext(request))
else:
form = CheckoutForm()
return render_to_response('classifieds/checkout.html', {'ad': ad, 'form': form}, context_instance=RequestContext(request))
谢谢你的建议。