Django:如何在视图请求之外获取已登录的用户? [英] Django: How can i get the logged user outside of view request?
问题描述
我有一个需要登录用户信息的类方法(在视图之外).如何在不将 request 传递给方法的情况下检索登录用户?不幸的是,我找不到一个好的解决方案,而且不存在这样的方法是不合逻辑的,因为 Django 应该将登录用户存储在某个地方.否则(我相信)不可能使用 django.contrib.auth.decorators
中的 @login_required
装饰器.对吗?
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. Otherwise (i believe) it would be impossible to use @login_required
decorator from django.contrib.auth.decorators
. Right?
所以如果不可能,为什么不呢?如果我唯一想要的是登录用户而不是 request
中的所有信息,为什么 Django 会这样工作?
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
?
提前致谢.
推荐答案
关于装饰器,错了.实际上装饰器使用请求参数调用.
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.
这是我们使用的代码.您需要将此中间件添加到 MIDDLEWARES 中.和进口 &调用 get_request 函数.
Here is the code that we use. You need to add this middleware to MIDDLEWARES. And import & calling get_request function.
2017 年 7 月更新: 使用 Python 3.6.1 和 Django 1.10.7 进行测试,基于此答案中的原始代码和 编写自己的中间件文档.
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.
- 首先创建一个新的应用程序,即.
startapp request_middleware
. - 然后将
"request_middleware"
添加到settings.py
中的INSTALLED_APPS
. - 然后粘贴下面的代码,即.
request_middleware.middleware.py
. - 最后在
settings.py
中将"request_middleware.middleware.RequestMiddleware"
添加到您的MIDDLEWARE
(在我的情况下,我已经将它在'debug_toolbar.middleware.DebugToolbarMiddleware'
和'django.middleware.security.SecurityMiddleware'
之间,尽我所能地高于列表).
- First create a new app, ie.
startapp request_middleware
. - Then add
"request_middleware"
to yourINSTALLED_APPS
insettings.py
. - After that paste the code bellow in, ie.
request_middleware.middleware.py
. - Finally add
"request_middleware.middleware.RequestMiddleware"
to yourMIDDLEWARE
insettings.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.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
之后,只需执行 from request_middleware.middleware import get_request
即可在您的代码中使用 get_request
.
After that simply do from request_middleware.middleware import get_request
in order to use get_request
in your code.
这篇关于Django:如何在视图请求之外获取已登录的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!