【问题标题】:How to get extended User custom fields in django template?如何在 django 模板中获取扩展的用户自定义字段?
【发布时间】:2016-12-20 17:00:53
【问题描述】:

我已经扩展了 django 内置用户模型。我目前正在使用 django 1.10。

我正在尝试在模板中显示用户名 short_name。 注意:short_name 是扩展字段

<span> {{ request.user.profile.username  }} </span>
<span> {{ user.profile.username  }} </span>
{{ user.get_username }}
{{ user.username }}

此模板标签不适用于 django 1.10

我已使用此链接创建自定义用户模型https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html

使用这个我想显示基于角色的内容。

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)

    employee_name = models.CharField(max_length=50)
    short_name = models.CharField(max_length=50)
    access = (
        ('admin', 'admin'),

        ('director', 'director'),
        ('management', 'management'),
        ('employee', 'employee'),
    )
    access_control = models.CharField(max_length=50, choices=access)
    YES_NO = (
        ('yes', 'Yes'),
        ('no', 'No'),
    )
    active = models.CharField(max_length=50, choices=YES_NO)

在设置中添加以下行:

AUTH_PROFILE_MODULE = "user_login.UserProfile"

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
"django.contrib.auth.context_processors.auth",
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

【问题讨论】:

    标签: django


    【解决方案1】:

    如果你有一个ForeignKeyuser 有关系,你不能通过user.userprofile.short_name 访问它,如果你没有在你的领域定义related_name,你可以通过user.userprofile_set.first().short_name 得到它,现在,你可以将关系类型更改为OneToOneField并用作user.userprofile.short_name,这种方式只适用于onetoone关系

    【讨论】:

    • 我已将模型迁移到 user = models.OneToOneField(User, on_delete=models.CASCADE) 但即使现在它对我也不起作用,即使 {{ request.user.username }} 这不起作用。除了上下文之外,我还需要向模板发送任何内容吗?
    • 必须是{{ request.user.userprofile.short_name }},如果你使用{{ request.user.username }},你指的是用户模型的用户名字段
    【解决方案2】:

    首先,从 User 到 UserProfile 的关系称为userprofile

    其次,username 在主用户模型上;您的额外字段称为short_name。所以:

    {{ user.userprofile.short_name }}
    

    【讨论】:

    • {{ user.userprofile.short_name }} 对我不起作用,可能是我的设置有问题
    • 正如 German 指出的那样,您有一个 ForeignKey 而不是 OneToOneField。这没有意义,你应该把它改成一对一,然后这个语法就行了。
    【解决方案3】:

    用户名字段在用户模型中,所以像{{ request.user.profile.username }} 这样的东西应该是{{ request.user.username }}。您的配置文件模型称为 UserProfile 并且您没有指定反向关系名称,因此默认情况下它将称为 userprofile,因此是 {{ user.userprofile.short_name }},而不是 {{ user.profile.short_name }}。请记住,要在模板中使用任何对象,您需要在上下文中传递它。

    【讨论】:

      猜你喜欢
      • 2011-02-22
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 2013-08-03
      相关资源
      最近更新 更多