Nikita
Nikita

Reputation: 75

Python Django How display information of logged user contained in another class in my template?

I'm developing a website in Python Django. I have CustomUser class for logging and another class coach where I have information of my users. Every user are linked to coach class.

I'm trying to display in my template information of my user contained in the coach class. I have logged user and can display their username on my template with {{ user.username }}

My problem is that I am not able to display, for example, Address with {{ coach.Adresse }}.

Any idea how to fix this?

My models.py

class coach(models.Model):
    user = models.OneToOneField(CustomUser,on_delete=models.CASCADE)
    Adresse = models.TextField(max_length=140, default='DEFAULT VALUE')
    Telephone = models.IntegerField(null=True, max_length=140, default='111')

My profile.html

<body>
{% if user.is_authenticated %}
Welcome {{ user.username }} !!! {{coach.Adresse}} !!!
{% else %}
    Not logged
{% endif %}
</body>

my views.py

def Profile(request):
        return render(request, 'Profile.html')

Upvotes: 1

Views: 352

Answers (1)

Alasdair
Alasdair

Reputation: 308779

Since you already have the user in the template context, you can follow the one-to-one field backwards with:

{{ user.coach.Adresse}} 

Upvotes: 1

Related Questions