Python 带有Django Rest Framework JWT的手动令牌
我目前正在使用Django Rest Framework JWT对一个项目进行身份验证。我已经实现了BasicAuthentication、SessionAuthentication和JSONWebTokenAuthentication,用户可以在每个新会话中使用POST方法请求令牌。但是,我希望在创建每个用户之后立即创建令牌(并且可能可以在管理部分查看) 我查看了Django Rest Framework JWT文档,其中指出可以使用以下方法手动创建令牌:Python 带有Django Rest Framework JWT的手动令牌,python,django,django-rest-framework,json-web-token,django-rest-auth,Python,Django,Django Rest Framework,Json Web Token,Django Rest Auth,我目前正在使用Django Rest Framework JWT对一个项目进行身份验证。我已经实现了BasicAuthentication、SessionAuthentication和JSONWebTokenAuthentication,用户可以在每个新会话中使用POST方法请求令牌。但是,我希望在创建每个用户之后立即创建令牌(并且可能可以在管理部分查看) 我查看了Django Rest Framework JWT文档,其中指出可以使用以下方法手动创建令牌: from rest_framewor
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
我尝试将此代码段放入views.py、models.py和serializers.py中,但在“user”上不断出现引用错误
任何关于如何正确实现此代码片段或替代方法的帮助都将不胜感激。谢谢我没有在官方文件上遵循这个例子。因为我在第二行和第三行都出错了。我的配置在我的设置路径上引发异常 我直接从库本身调用函数
from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
假设我的函数以1个dictionary作为输入,并返回标记
from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
def create_token(platform_data: typing.Dict):
"""
System will search from userprofile model
Then create user instance
:param platform_data:
:return:
"""
# If found `userprofile `in the system use the existing
# If not create new `user` and `userprofile`
platform_id = platform_data.get('id') # Can trust this because it is primary key
email = platform_data.get('email') # This is user input should not trust
userprofile_qs = UserProfile.objects.filter(platform_id=platform_id)
if userprofile_qs.exists():
# user exists in the system
# return Response token
userprofile = userprofile_qs.first()
user = userprofile.user
else:
# Create user and then bind it with userprofile
user = User.objects.create(
username=f'poink{platform_id}',
)
user.email = email # Get latest email
user.save()
UserProfile.objects.create(
platform_id=platform_id,
user=user,
)
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
return token
希望能从中得到启发我没有遵循官方文件中的示例。因为我在第二行和第三行都出错了。我的配置在我的设置路径上引发异常
我直接从库本身调用函数
from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
假设我的函数以1个dictionary作为输入,并返回标记
from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
def create_token(platform_data: typing.Dict):
"""
System will search from userprofile model
Then create user instance
:param platform_data:
:return:
"""
# If found `userprofile `in the system use the existing
# If not create new `user` and `userprofile`
platform_id = platform_data.get('id') # Can trust this because it is primary key
email = platform_data.get('email') # This is user input should not trust
userprofile_qs = UserProfile.objects.filter(platform_id=platform_id)
if userprofile_qs.exists():
# user exists in the system
# return Response token
userprofile = userprofile_qs.first()
user = userprofile.user
else:
# Create user and then bind it with userprofile
user = User.objects.create(
username=f'poink{platform_id}',
)
user.email = email # Get latest email
user.save()
UserProfile.objects.create(
platform_id=platform_id,
user=user,
)
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
return token
希望能从中得到启发