Django, request.user is always Anonymous User

后端 未结 9 2083
无人共我
无人共我 2020-12-01 14:57

I am using a custom authentication backend for Django (which runs off couchdb). I have a custom user model.

As part of the login, I am doing a request.user = u

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 15:35

    In case you are using an API (Django-rest-framework) and accessing a view using a get, post, etc. methods.

    You can get a user by sending the Bearer/JWT token corresponding to that user.

    Wrong

    # prints Anonymous User
    
    def printUser(request):
       print(request.user)
    

    Correct

    # using decorators 
    # prints username of the user 
    
    @api_view(['GET'])  # or ['POST'] .... according to the requirement
    def printUser()
        print(request.user)
    

提交回复
热议问题