Python reactjs如何从django访问会话数据(用户名)

Python reactjs如何从django访问会话数据(用户名),python,django,session,reactjs,Python,Django,Session,Reactjs,我将django与reactjs一起使用,现在我已经维护了会话,并希望通过reactjs访问会话数据,以使用cutomise UI。 我能为此做些什么 //my models.py snippet class Users(models.Model): gender = models.CharField(max_length=200) username = models.CharField(max_length=50) password = models.CharFiel

我将django与reactjs一起使用,现在我已经维护了会话,并希望通过reactjs访问会话数据,以使用cutomise UI。 我能为此做些什么

//my models.py snippet

class Users(models.Model):
    gender = models.CharField(max_length=200)
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=50)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    bg = models.CharField(max_length=200)
    badges = models.BigIntegerField(null=True)
    dob = models.DateField()
    contact = models.BigIntegerField(null=True)
    age = models.BigIntegerField(default=0)
    status = models.BigIntegerField(null=True)




//my views.py snippet

def login(request):
    print(request.POST.get('username'))
    username=str(request.POST.get('username'))
    password=str(request.POST.get('password'))

    if(Users.objects.filter(password=password, username=username).exists()):
        request.session['username'] = username
        name=request.session['username']
        return render(request, 'loggedin.html', {"username" : name})
    else:
        return HttpResponse("incorrect data")   

一种方法是遵循GitHub上Scott Woodall的命令。
基本上,他的方法是将用户请求数据填充到Django的'base.html'(在项目中称为app.html),由后端(Django)提供服务

在前端,他通过“窗口”对象访问数据

{% load static %}
<!-- This is basically the ONLY webpage created in Django
see the window.django = {} part where variables are handed over initially to React
top most the Django CSRF Token
is this secure in Production?
...
...
-->

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Django & React Starter</title>
        ...
        ...
    </head>
    <body>
        <noscript>This application requires javascript to function.</noscript>
        <div id="root"></div>
        <script src="{% static "js/vendor.bundle.js" %}"></script>
        <script>
        window.django = {
            csrf: "{{ csrf_token }}",
            urls: {
                logout: "{% url "logout" %}",
                staticRoot: "{% static "" %}",
                users: "{% url "emailuser-list" %}"
            },
            user: {
                username: "{{ request.user.username }}",
                full_name: "{{ request.user.get_full_name }}",
                last_login: "{{ request.user.last_login }}",
                permissions: immutable.Set(
                    {% autoescape off %}JSON.parse('{{ permissions }}'){% endautoescape %}
                )
            }
        };
        </script>
        <script src="{% static "js/bundle.js" %}"></script>
    </body>
</html>
{%load static%}
Django&React起动器
...
...
此应用程序需要javascript才能运行。
window.django={
csrf:“{{csrf_令牌}}”,
网址:{
注销:“{%url”注销“%}”,
staticRoot:“{%static”“%}”,
用户:“{%url”电子邮件用户列表“%}”
},
用户:{
用户名:“{request.user.username}}”,
全名:“{{request.user.get_full_name}”,
上次登录:{{request.user.last_login}},
权限:不可变。设置(
{%autoescape off%}JSON.parse({{permissions}}'){%endautoescape%}
)
}
};

斯科特·伍德奥的所有功劳/我希望这能让你开始

只是一个建议。。不要在前端开发中混用react和django。。在前端使用react/redux,在后端使用django rest。我在处理一个涉及此场景的项目时遇到了很多麻烦,最终不得不在前端使用Complete react。您可以使用Django Rest框架创建一个端点,该端点使用用户信息进行响应-端点接收正常的请求对象,因此您只需返回Request.user.username,等等。这就是我们在应用程序中所做的,它工作得非常好。最初,我们考虑在js应用程序初始化时调用服务器获取一大块JSON,但这太复杂了。这种方法很简单,而且效果很好。