Django REST: make request.session['key'] available on all views

I'm trying to check if a user logged in saving a token in request.session["token"]. On login, the request.session["token"] is set and the print() works. How do I make that key available on all views?

def login(request):
  if["usuario"] and["pwd"]:
      user_item = User.objects.get(["usuario"])
    except User.DoesNotExist:
      return Response({'errors': 'Usuario no existe'}, status=402)
    usr_pwd =["pwd"]
    bd_pwd = user_item.pwd
    if bcrypt.checkpw(usr_pwd.encode('utf-8'),bd_pwd.encode('utf-8')):
      token = jwt.encode({'user':["usuario"]}, 'palacin', algorithm='HS256')
      request.session["token"] = token
      print(request.session["token"]) # <-- THIS PRINT WORKS
      return Response(200)
      return Response({'errors': 'Usuario y/o contraseña incorrectos.'}, status=401)
    return Response({'errors': 'Usuario y/o contraseña no especificados'}, status=400)

def getUser(request):
  print(request.session["token"]) # <-- THIS PRINT DOESN'T WORK
  '''if "token" in request.session:
    return Response(jwt.decode(request.session["token"], 'palacin', algorithms=['HS256']))
    return Response({'errors': 'Sesión no iniciada'}, status=403)'''

Back to Top