相关疑难解决方法(0)

使用request.user的Django和Middleware总是匿名的

我正在尝试制作中间件,根据子域等改变用户的某些字段......

唯一的问题是request.user总是在中间件中作为AnonymousUser提供,但是在视图中是正确的用户.我已经离开了django在设置中使用的默认身份验证和会话中间件.

这里有一个类似的问题:Django,request.user总是匿名用户 但是没有过度回答整个问题,因为我没有使用不同的身份验证方法,并且在调用我自己的中间件之前djangos身份验证正在运行.

有没有办法在使用DRF时获取中间件中的request.user?我将在这里展示一些示例代码:

class SampleMiddleware(object):

  def process_view(self, request, view_func, view_args, view_kwargs):
    #This will be AnonymousUser.  I need it to be the actual user making the request.
    print (request.user)    

  def process_response(self, request, response):
    return response
Run Code Online (Sandbox Code Playgroud)

使用process_request:

class SampleMiddleware(object):

  def process_request(self, request):
    #This will be AnonymousUser.  I need it to be the actual user making the request.
    print (request.user)    

  def process_response(self, request, response):
    return response
Run Code Online (Sandbox Code Playgroud)

python django django-rest-framework

29
推荐指数
5
解决办法
2万
查看次数

标签 统计

django ×1

django-rest-framework ×1

python ×1