[英]Django: How can i get the logged user outside of view request?
I have a class method (outside of a view) who needs the logged user's information.我有一个需要登录用户信息的类方法(在视图之外)。 How can i retrieve the logged in user without passing the request to the method?
如何在不将请求传递给方法的情况下检索登录用户? Unfortunately i can't find nowhere a nice solution and it's not logical just not exist such a way to do it, because Django should store somewhere the logged in user.
不幸的是,我找不到一个好的解决方案,而且不存在这样的方法是不合逻辑的,因为 Django 应该将登录用户存储在某个地方。 Otherwise (i believe) it would be impossible to use
@login_required
decorator from django.contrib.auth.decorators
.否则(我相信)不可能使用来自
django.contrib.auth.decorators
@login_required
装饰器。 Right?对?
So if it's not possible why it's not?那么,如果不可能,为什么不呢? Why Django works like this if the only think i want is the logged in user and not all the informations inside
request
?如果我唯一想要的是登录用户而不是
request
所有信息,为什么 Django 会这样工作?
Thanks in advance.提前致谢。
About decorators, it is wrong.关于装饰器,这是错误的。 Actually decorators called with request argument.
实际上装饰器用请求参数调用。
I believe better way is that passing user or request object to class's method.我相信更好的方法是将用户或请求对象传递给类的方法。 But there are other ways to access request.
但是还有其他方法可以访问请求。
Here is the code that we use.这是我们使用的代码。 You need to add this middleware to MIDDLEWARES.
您需要将此中间件添加到 MIDDLEWARES。 And import & calling get_request function.
并导入和调用 get_request 函数。
Update July 2017: Tested with Python 3.6.1 and Django 1.10.7, based in the original code from this answer and in the Writing your own middleware documentation. 2017 年 7 月更新:使用 Python 3.6.1 和 Django 1.10.7 进行测试,基于此答案中的原始代码和编写您自己的中间件文档。
startapp request_middleware
. startapp request_middleware
。"request_middleware"
to your INSTALLED_APPS
in settings.py
."request_middleware"
添加到settings.py
的INSTALLED_APPS
中。request_middleware.middleware.py
. request_middleware.middleware.py
。"request_middleware.middleware.RequestMiddleware"
to your MIDDLEWARE
in settings.py
(In my case I've placed it in between 'debug_toolbar.middleware.DebugToolbarMiddleware'
and 'django.middleware.security.SecurityMiddleware'
as far above the list as I could)."request_middleware.middleware.RequestMiddleware"
添加到settings.py
的MIDDLEWARE
(在我的情况下,我将它放在'debug_toolbar.middleware.DebugToolbarMiddleware'
和'django.middleware.security.SecurityMiddleware'
,远高于列表我可以)。# request_middleware.middleware.py
from threading import current_thread
_REQUESTS = {}
class RequestNotFound(Exception):
def __init__(self, message):
self.message = message
def get_request():
thread = current_thread()
if thread not in _REQUESTS:
raise RequestNotFound('global request error')
else:
return _REQUESTS[thread]
class RequestMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def process_request(self, request):
_REQUESTS[current_thread()] = request
def __call__(self, request):
self.process_request(request)
response = self.get_response(request)
return response
After that simply do from request_middleware.middleware import get_request
in order to use get_request
in your code.之后,只需执行
from request_middleware.middleware import get_request
即可在您的代码中使用get_request
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.