如何在python(Django)中获取csrf_token

时间:2021-08-31 06:38:43


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

我想构建一个cordova应用程序,我想在应用程序上使用表单(django后端)。

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

我的问题是如何在python中获取csrf_token所以我可以序列化并将其发送到javascript?

Any help will be apreciated.
Thank you

任何帮助都会被贬低。谢谢

2 个解决方案

#1


1  

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

#2


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.

CSRF令牌存在于{{csrf_token}}下的模板呈现上下文中。如果您在模板中内联Javascript,则可以呈现它。

<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.

另一种方法是从javascript访问cookie(信用卡塞尔加西亚)。方法是在他的链接中。

#1


1  

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

#2


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.

CSRF令牌存在于{{csrf_token}}下的模板呈现上下文中。如果您在模板中内联Javascript,则可以呈现它。

<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.

另一种方法是从javascript访问cookie(信用卡塞尔加西亚)。方法是在他的链接中。