从 window.LocalStorage 访问用户 ID
Accessing User ID from window.LocalStorage
我有一个 DjangoRestFramework / Vue.js 应用程序。
现在我正在使用 this.requestUser = window.localStorage.getItem("username");
获取我的登录用户
我只需要 requestUser
来检查用户是否已登录。
现在我必须通过端点“POST”数据,但在我的后端,DRF 期望 ID
而不是 string
值。
所以我用 'developer tool' 检查了我的 window.localStorage 并且只存储了我的用户名值,而不是它的值。
我没有几个选择:
-create a new endpoint that will fetch an user ID from its value. And then on created()
run a function that will save it into
window.localStorage
-modify my models and serializers so I can use my user value instead of its ID to interact with my endpoints.
-or simply there is a configuration that I am not aware of than can save my user.id
as well as my user.username
在 DRF 中,我使用纯 django.contrib.auth
作为我的用户模型。解决我的问题的最佳方法是什么?
由于您使用的是身份验证,因此您可以在后端从 request.user.id
获取 id
。
request.user
typically returns an instance of
django.contrib.auth.models.User
我有一个 DjangoRestFramework / Vue.js 应用程序。
现在我正在使用 this.requestUser = window.localStorage.getItem("username");
获取我的登录用户
我只需要 requestUser
来检查用户是否已登录。
现在我必须通过端点“POST”数据,但在我的后端,DRF 期望 ID
而不是 string
值。
所以我用 'developer tool' 检查了我的 window.localStorage 并且只存储了我的用户名值,而不是它的值。
我没有几个选择:
-create a new endpoint that will fetch an user ID from its value. And then on
created()
run a function that will save it intowindow.localStorage
-modify my models and serializers so I can use my user value instead of its ID to interact with my endpoints.
-or simply there is a configuration that I am not aware of than can save my
user.id
as well as myuser.username
在 DRF 中,我使用纯 django.contrib.auth
作为我的用户模型。解决我的问题的最佳方法是什么?
由于您使用的是身份验证,因此您可以在后端从 request.user.id
获取 id
。
request.user
typically returns an instance ofdjango.contrib.auth.models.User