How to Get Only CSRF Token Value in Django

When generating a CSRF token in Django you'll notice that it automatically creates a hidden input element:

 

{% csrf_token %}
<input type="hidden" name="csrfmiddlewaretoken" value="Ud7sPKUp8JBlCedR3GYVNLBjSe7mtQjyIRgQ4sCYRoVBAYlPs9HJrCzqNlYBkCiV">

 

Sometimes we just need to get the CSRF token without anything else.

 

To get only the CSRF token with no HTML in Django to use in a meta tag .etc call csrf_token like this:

 

{{ csrf_token }}

 

One example use case of this is to create a custom Django CSRF token meta tag:

 

<meta name="csrf" content="{{ csrf_token }}">