【问题标题】:how can reactjs access session data(username) from djangoreactjs如何从django访问会话数据(用户名)
【发布时间】:2017-02-09 07:36:28
【问题描述】:

我正在使用带有 reactjs 的 django,现在我已经维护了会话,并希望通过 reactjs 访问会话数据以减少 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")   

【问题讨论】:

  • 只是一个建议.. 不要将 react 和 django 混合用于前端开发.. 用于前端的 react/redux 和后端的 django-rest。我在处理涉及这种情况的项目时遇到了很多麻烦,最终不得不在前端使用完全反应。
  • 您可以使用 Django Rest 框架来创建一个响应用户信息的端点 - 端点接收正常的请求对象,因此您可以只返回 request.user.username 等。

标签: python django session reactjs


【解决方案1】:

执行此操作的一种方法是关注 GitHub 上 Scott Woodall 的 django-react-template
基本上,他的方法是将用户请求数据填充到 Django 的 'base.html'(在项目中称为 app.html),由后端 (Django) 提供服务 p>

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

{% 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/ 我希望这能让你开始!

【讨论】:

  • 这就是我们在我们的应用程序中所做的,而且效果非常好。最初,我们考虑在我们的 js 应用程序初始化时调用服务器获取一段 JSON,但这太复杂了。这种方法很简单,而且效果很好。
猜你喜欢
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 2012-08-26
  • 2013-08-30
  • 2019-08-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
相关资源
最近更新 更多