Question

How can I embed django csrf token straight into HTML?

Is there a way to insert the the csrf token directly from within the Python files I'm editing? The token is different for each session, so storing it in the DB is not very useful. Is there a way to dynamically load the token within the view?

 45  46221  45
1 Jan 1970

Solution

 57

Call django.middleware.csrf.get_token(request) to get the CSRF token.

2010-07-21

Solution

 52

The way to use it, is to use it directly in the templates.

From the documentation,:

<form action="" method="post">
{% csrf_token %}

is all you have to include.

2010-07-20