[Django]-How to get the csrf_token in python(Django)

4πŸ‘

βœ…

I just got the correct answer .. thanks everybody for helping.

from django.shortcuts import render_to_response
from django.template.context_processors import csrf

def my_view(request):
    csrf = unicode(csrf(request)['csrf_token'])
πŸ‘€Rus Mine

0πŸ‘

The CSRF token is present in the template rendering context under {{ csrf_token }}. If you inline the Javascript in a template, you can render it.

<html>
<head>
<script language="JavaScript">
var csrfToken = "{{ csrf_token }}";
</script>
</head>
</html>

Alternative is to access the cookie from javascript (credits Sergio Garcia). The how-to is in his link.

Leave a comment