Django:如何在模板中获取Userprofile实例,与auth.User模型具有OneToOne关系?

时间:2015-08-23 14:01:33

标签: python django django-users

我的模型中有一个UserProfile,与用户模型有OneToOne关系。

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')
    slug = models.SlugField(unique=True)
    birthday = models.DateField(blank=False)
    img = models.ImageField(upload_to='profile_images', blank=True, null=True)

在我的模板中,我需要获取用户的数据,但不仅包括用户名,电子邮件和姓名,这些是用户的字段,还包括生日和图像。正如我在数据库中看到的那样,用户数据和userprofile数据是两个不同的表。

但是,我无法获得Userprofile类的实例。 Hello, {{ user.first_name }}!有效。我很害怕,我不了解在模板中使用当前实例的一些概念。你能解释一下吗?

所以,我试过这个:

编辑:

This is a user birthday: {{ user.birthday }} #doesn't work
This is a user birthday: {{ user.profile.birthday }} #WORKS!
This is a user birthday: {{ userprofile.birthday }} #doesn't work

请帮帮我。我花了太多时间来理解这一点,但没有成功。

0 个答案:

没有答案