简体   繁体   English

添加到django request.user变量

[英]Add to the django request.user variable

In django, I use the authenticate() function to log users in. Using request.user , I can access the logged in user's information which is pulled from the users table. 在django中,我使用authenticate()函数登录用户。使用request.user ,我可以访问从用户表中提取的登录用户信息。 If I do request.user.id , I can get the user's id. 如果我执行request.user.id ,则可以获取用户的ID。 In any template, on any request, I can do user.username to get the username. 在任何模板中,根据任何请求,我都可以执行user.username来获取用户名。

Is there a way by which I can add values to the request.user variable so that I can use them throughout my application and in templates? 有没有一种方法可以将值添加到request.user变量,以便可以在我的应用程序和模板中使用它们?

For example, I would like to have this: request.user.timezone set when the user logs in to the site; 例如,我想设置以下内容:用户登录网站时设置request.user.timezone I should be able to access this variable in templates and in my views. 我应该能够在模板和视图中访问此变量。

You can write a simple middleware: 您可以编写一个简单的中间件:

class UserTZMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        if request.user.is_authenticated():
            request.user.timezone = evaluate_tz()

and then append this into MIDDLEWARE_CLASSES in your settings.py file: 然后将其附加到settings.py文件中的MIDDLEWARE_CLASSES中:

MIDDLEWARE_CLASSES = (
    ....,
    'myapp.UserTZMiddleware',
)

Would you like to let the user change it time-zone and force a specific time-zone ? 您想让用户更改时区并强制指定时区吗? I assume you would, if so you would like to keep the timezone configurable in your database. 如果可以的话,我想您想在数据库中保持可配置的时区。 Therfore I would suggest you to make a UserProfile model which has one to one relations with the User Object. 因此,我建议您制作一个与用户对象具有一对一关系的UserProfile模型。

You can do : 你可以做 :

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    time_zone = models.whateverField()
    ... add anything else you would like to extend your profile ...

On your templete you will be able to get the timezone by doing that : 在您的Templete上,您将可以通过以下操作获取时区:

{{ request.user.userprofile.time_zone }}

Simple and very clean. 简单而且很干净。

If you would like to have an automatic creation of your userprofile whenever you save a User object, you can use signals for that. 如果您希望在保存用户对象时自动创建用户配置文件,则可以为此使用信号。

Add the request context processor to your TEMPLATE_CONTEXT_PROCESSORS. 将请求上下文处理器添加到您的TEMPLATE_CONTEXT_PROCESSORS。 This processor adds the request to the context, in your template you can do {{request}} or {{ request.user }} 该处理器将请求添加到上下文中,您可以在模板中执行{{request}}或{{request.user}}

see: https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request 参见: https : //docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request

Use sessions , which are designed to keep variables in the context. 使用session ,旨在将变量保留在上下文中。

You should not change request.user as this is something that other applications will rely on and one that is populated by django's own auth machinery. 您不应该更改request.user因为这是其他应用程序将依赖的内容,并且由django自己的身份验证机制填充。

If you need to keep track of the timezone specifically, you should read up on the timezone support in django 1.4. 如果需要特别跟踪时区,则应阅读django 1.4中的时区支持 In the documentation it provides examples for setting and managing timezones for users. 在文档中,它提供了用于设置和管理用户时区的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM