B
B
bash772015-12-22 12:09:59
Django
bash77, 2015-12-22 12:09:59

Python & Django / CSRF validation failed. Request denied. what's wrong?

good day everyone! I know the python well, but I'm only torturing Django for the second day. and the second day on this error.
So: the project was created according to the manual. I create a form, inside the form I write {% csrf_token %} in the default settings 'django.middleware.csrf.CsrfViewMiddleware' is enabled.
I render in a view like this:

def index(request):
    cats = CategoryQuestion.objects.all().order_by("name")
    return render(request, "index.html", {"cats": cats})

the token arrives in cookies, in the form there is also a token code and it matches the cookie one. but when you click on the Submit button - CSRF validation error. Request denied. I'm using the built-in developer web server. version of django 1.9 what's the trouble tell me. I was tired already, I re-read the dock once again and nothing helped.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bash77, 2015-12-22
@bash77

Mistake won! in the template the form started like this:

<form method="post" action="testing/"  enctype="text/plain">
{% csrf_token %}
...

removed enctype="text/plain" - everything worked! thanks for the help!

G
Gasoid, 2015-12-22
@Gasoid

perhaps the field name is not csrfmiddlewaretoken ? or send from another domain
, see what leaves the browser in the console

B
belanchuk, 2015-12-22
@belanchuk

I encountered this during development.
To eliminate this error during development, put the @csrf.csrf_protect decorator on the view that submits the form.
Remove this decorator when deploying.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question