E
E
Evgeny Elizarov2013-10-15 10:49:47
Django
Evgeny Elizarov, 2013-10-15 10:49:47

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.

I go to the proposed man , I try all 5 proposed methods, and none of them help. what am I doing wrong?

with the help of barker the issue is resolved, it was necessary to use render() instead of render_to_response()

Answer the question

In order to leave comments, you need to log in

6 answer(s)
B
barker, 2013-10-15
@KorP

No 5 methods needed. You just need to enable the appropriate middleware and insert the csrf_token in the template.

M
Mikhail Priver, 2013-10-15
@mpriver

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-токен">

3. When submitting the form, the cookie and CSRF token goes back to Django along with the rest of the form fields.
4. Django checks the token from the cookie and from the POST.

M
merlin-vrn, 2013-10-15
@merlin-vrn

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).

A
alz, 2013-10-15
@alz

{% csrf_token %} used in the template?

K
kenny_opennix, 2013-10-15
@kenny_opennix

what middleware are connected?

N
navisr, 2016-12-05
@navisr

Who needs a chewed up answer with examples: https://bovs.org/post/179/Zasita-ot-CSRF-v-Django-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question