我試圖創建一個基於Django Classifieds App一個Django應用程序,但我得到嘗試提交表單時的錯誤:CSRF verification failed. Request aborted. Reason given for failure: CSRF token missing or incorrect.
」CSRF驗證失敗,CSRF令牌丟失或不正確。「 Django的
我確實有形式{% 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
我也有我的settings.py包含在MIDDLEWARE_CLASSES 'django.middleware.csrf.CsrfViewMiddleware',
我還有什麼可以錯過正確提交表單?
函數看起來象:
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))
謝謝您的建議。
你有CSRF中間件和背景處理器? – DrTyrsa
Chech如果你有要求中間件/裝飾和其他東西... https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/#how-it-works – FallenAngel
是的,我確實有' django.middleware.csrf.CsrfViewMiddleware'在我的settings.py中的MIDDLEWARE_CLASSES中。還有什麼我可能會錯過的?謝謝 –