Access request.session from backend.get_user

前端 未结 3 1090
谎友^
谎友^ 2020-12-14 22:46

first of all: this is not the same as this. The ModelBackend has no request member.

I want to access the session of the current user without ac

相关标签:
3条回答
  • 2020-12-14 23:04

    You can also pass a 'request' parameter to the authenticate function any time you call it:

    def authenticate(self, request, **kwargs):
        ....
    
    0 讨论(0)
  • 2020-12-14 23:09

    You can use the tiny ThreadLocalMiddleware. It makes the request object everywhere available.

    from django_tools.middlewares import ThreadLocal
    
    request = ThreadLocal.get_current_request()
    # request.session <-- is now available
    

    Do not forget to add the middleware into the MIDDLEWARE_CLASSES tuple of your settings.py:

    MIDDLEWARE_CLASSES = (
        ...
        'django_tools.middlewares.ThreadLocal.ThreadLocalMiddleware',
        ...
    )
    
    0 讨论(0)
  • 2020-12-14 23:25

    The RemoteUserBackend (from django.contrib.auth.backends) uses special middleware, RemoteUserMiddleware, to access request data. Maybe you could do it this way, too.

    0 讨论(0)
提交回复
热议问题