求教:关于django+vue前后端分离项目token传递保存的问题

向各位佬大求教: 在做django+vue项目时,django后台通过前端post请求获取用户登录信息后,通过authenticate进行验证,login() 登录后,接下来是如何获取token,并传递给vue前端服务器的? 是不是JsonResponse?

        谢谢!

评论 1

  • from utils.jwt_auth import create_token,parse_payload

    登录验证

    def login_user(request): if request.method == "POST": data = json.loads(request.body) username = data.get("username") #获取密码 password = data.get("password") if username is not None and password is not None: islogin = authenticate(username=username, password=password) if islogin: token = create_token({"username":username}) return JsonResponse({"code": 0, "message": "登录成功!", "data":{"access_token": token,}}) else: return JsonResponse({"code": 1, "message": "登录失败, 请检查用户名或者密码是否输入正确."}) else: return JsonResponse({"code": 1, "message": "用户名或密码为空,请检查"}) else: return JsonResponse({"code": 1, "message": "参数错误!"})