简体   繁体   English

Django:如何在视图请求之外获取登录用户?

[英]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 进行测试,基于此答案中的原始代码和编写您自己的中间件文档。

  • First create a new app, ie.首先创建一个新的应用程序,即。 startapp request_middleware . startapp request_middleware
  • Then add "request_middleware" to your INSTALLED_APPS in settings.py .然后将"request_middleware"添加到settings.pyINSTALLED_APPS中。
  • After that paste the code bellow in, ie.之后粘贴下面的代码,即。 request_middleware.middleware.py . request_middleware.middleware.py
  • Finally add "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.pyMIDDLEWARE (在我的情况下,我将它放在'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.

相关问题 Django当我使用基于类的视图时,如何在表单中传递request.user(当前登录的用户)? - Django How I can pass request.user(current logged user) in forms when I use class based view? 如何在 Django 中获取登录用户的用户名? - How can I get the username of the logged-in user in Django? 如何在 Django 中获取登录用户的用户名? - How can I get the username of the logged in user in Django? 如何将django中当前登录的用户配置文件添加到模型中? - How can I get current logged user profile in django, into models? 我如何在 django 的请求之外访问当前用户 - How can i access the current user outside a request in django 如果登录Django,如何获取用户ID? - How I get User ID if logged in Django? 如何请求已登录的用户确认密码以进入 django 中的特定页面? - How can I request an already logged in user to confirm password to enter a specific page in django? Django:如何在基于 class 的视图中获取登录用户的用户名? - Django: How do I get the username of the logged-in user in my class based view? 在不传递请求参数的情况下,如何获取 django 中的登录用户名 - Without passing Request parameter How Can i get the Logged in Username in django 如何从django-oauth-toolkit令牌获取当前登录用户? - How can I get the current logged in user from a django-oauth-toolkit token?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM