Django将请求['user']存储在哪个fi中

2024-03-28 19:58:37 发布

您现在位置:Python中文网/ 问答频道 /正文

如文档中所述,已验证用户的对象存储在模板中的用户变量中。我需要django在应用程序文件中存储用户变量的位置谢谢:

user = request.user
request['user'] = user #where is?

谢谢你的帮助


Tags: 文件对象django用户文档模板应用程序is
2条回答

确保您正在使用^{}。否则user在模板中不可用。你知道吗

AuthenticationMiddleware里。你知道吗

官方文件提到:

link

AuthenticationMiddleware associates users with requests using sessions.

link

class AuthenticationMiddleware

Adds the user attribute, representing the currently-logged-in user, to every incoming HttpRequest object. See Authentication in Web requests.

源代码(django.contrib.auth公司.中间件.py)地址:

class AuthenticationMiddleware(object):
    def process_request(self, request):
        assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."

        request.user = SimpleLazyObject(lambda: get_user(request))

相关问题 更多 >