django - django 登录后我需要什么进行身份验证?
问题描述
我按照以下示例进行操作: https ://docs.djangoproject.com/en/3.0/topics/auth/default/#django.contrib.auth.login
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
此代码似乎没有向客户端返回任何令牌或某种类型,而只是一个重定向。
在 django 检查身份验证的其他代码中,django 检查request.user.is_authenticated
. 我需要自己设置吗?
from django.conf import settings
from django.shortcuts import redirect
def my_view(request):
if not request.user.is_authenticated:
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
要获得装饰器的身份验证,例如@login_required
或具有适当的值request.user.is_authenticated
,我需要从客户端(ReactJS)发送和接收什么?
解决方案
您可以返回一个 JsonResponse。
from django.http import JsonResponse
并且登录成功后
return JsonResponse ({
'token': user.token,
})
推荐阅读
- apache-kafka - 在 Kafka v 1.xx 及更高版本中,如果我有消息偏移量和分区,是否可以获得消息时间戳?
- javascript - 为什么 JavaScript 中的“(2.5 < 2.5 + Number.EPSILON)”为假?
- sonarqube - 如何在 sonar-project.properties 文件中传递带有空格的 xcode 项目名称
- javascript - 'this' 没有在 jquery 函数上选择
- excel - 如何根据标准排名并考虑重复值
- java - java - 在java中的selenium中查找元素列表时如何实现对webdriver.findElements()的隐式等待?
- android - 我们如何在 kotlin 中使用自定义字体创建 TextView 的自定义类?
- google-cloud-storage - 允许用户从浏览器上传文件到谷歌云存储
- java - 如何从url中获取一串字符
- python-3.x - 混合精度训练报告RET_CHECK失败,ShapeUtil::Equal(first_reduce->shape(), inst->shape())