简体   繁体   中英

How to get the csrf_token in python(Django)


I want to build a cordova application and i want to use forms(django backend) on the application.

My question is how to get the csrf_token in python so i can serialize it and send it to javascript?

Any help will be apreciated.
Thank you

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'])

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM