[英]How to get custom user model's custom fields in Django
I've created one custom user model as below 我创建了一个自定义用户模型,如下所示
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
Now I want to access that newly added fields(bio, location, birth_date) in my html template. 现在我想在我的html模板中访问新添加的字段(bio,location,birth_date)。 How can I do the same.
我怎么能这样做。
In your html, you can access to the users data like this: {{user.profile.bio}}
在您的html中,您可以访问以下用户数据:
{{user.profile.bio}}
Firsteval, you must pass your User
or your Profile
object to your context. Firsteval,您必须将您的
User
或您的Profile
对象传递给您的上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.