ergelo
ergelo

Reputation: 993

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?

Upvotes: 46

Views: 47111

Answers (2)

viam0Zah
viam0Zah

Reputation: 26312

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

Upvotes: 57

lprsd
lprsd

Reputation: 87095

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.

Upvotes: 53

Related Questions