Skip to main content
 首页 » 编程设计

python之reactjs 如何从 django 访问 session 数据(用户名)

2025年05月04日28lautakyan007

我正在将 django 与 reactjs 一起使用,现在我已经维护了 session 并希望通过 reactjs 访问 session 数据以削减 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")    

请您参考如下方法:

执行此操作的一种方法是遵循 django-react-template来自 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> 

所有功劳都归功于 Scott Woodall o/ 我希望这能让您入门!