我有一个 django 项目,其中有 2 个不同的登录页面。
- 位于着陆页中。例如:https ://website.com/
- 是 django 管理员登录。例如:https ://website.com/admin/ 对于这两个登录,我使用的是相同的 postgres 用户表。
要求是,当我从登陆登录页面登录时,如果用户有权访问管理员,他应该能够同时登录登陆页面和管理员页面,反之亦然。当我从两个登录中的任何一个注销时,如果两个登录的登录用户相同,它应该能够从两个登录中注销。
我在登录页面的登录和注销代码
@api_view(['POST'])
def login(request):
if request.method == 'POST' and request.data:
data = request.data
username = data['username']
password = data['password']
user = auth.authenticate(username=username, password=password)
if (user is not None) and (user.is_active):
auth.login(request, user)
token, created = Token.objects.get_or_create(user=user)
return Response({"username": str(user), "token": token.key, "status": STATUS['SUCCESS'], "message": MESSAGE_SUCCESS['LOGIN']}, status=status.HTTP_200_OK)
return Response({"status": STATUS['ERROR'], "message": MESSAGE_ERROR['LOGIN']}, status=status.HTTP_200_OK)
@api_view(['POST'])
@authentication_classes((TokenAuthentication,))
@permission_classes((IsAuthenticated,))
def logout(request):
try:
if request.method == 'POST' and request.data:
data = request.data
username = data['username']
password = data['password']
user = auth.authenticate(username=username, password=password)
auth.logout(request)
update_session_auth_hash(request, user)
return Response({"status": STATUS['SUCCESS'], "message": MESSAGE_SUCCESS['LOGOUT']}, status=status.HTTP_200_OK)
except:
return Response({"status": STATUS['ERROR'], "message": MESSAGE_ERROR['LOGOUT']}, status=status.HTTP_200_OK)
目前我只能做到这一点。也就是说,当我从登录页面登录时,如果他是管理员,它将自动登录到管理员登录,类似注销。
当我从管理员登录登录/注销时,我无法从登录页面登录登录或注销。