我遵循以下示例: 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)发送和接收什么?
答案 0 :(得分:0)
您可以返回JsonResponse。
from django.http import JsonResponse
成功登录后
return JsonResponse ({
'token': user.token,
})