Answer the question
In order to leave comments, you need to log in
Django: post request and CSRF issue
Hello. I'm just starting to understand django and stumbled over such a thing. I drew a simple form in the template, send it and get
CSRF verification failed. Request aborted.
Answer the question
In order to leave comments, you need to log in
No 5 methods needed. You just need to enable the appropriate middleware and insert the csrf_token in the template.
Should work without any tricks. If you are using the standard application template, all you need to do is paste {% csrf_token %}
it inside the form.
The mechanism itself in a simple case (without Ajax) works like this:
1. Django sets the cookie csrftoken=CSRF token
2. Instead of template_tag {% csrf_token %}
, Django inserts:
<input type="hidden" name="csrfmiddlewaretoken" value="CSRF-токен">
Look at your template, please. Compare with the HTML generated from this template. Check: what's in the HTML in place of {% csrf_token %} from the template? (Must be input type=hidden name=csrfmiddlewaretoken). Is this field sent to the server along with the form, i.e. is {% csrf_token %} inside <form>...</form>
? Intercept HTTP and check (you can tell firebug to see what is being sent).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question