Jand
Jand

Reputation: 2727

Weird behavior in Django template

I have this part in a user profile template:

    <p>{{ user.get_username }} = {{ profile.username  }} </p>

    {% if user.is_authenticated %}          
       {% if user.get_username != profile.username   %}
            This is the profile of another user             
       {% else%}
            This is your profile
     {% endif %}
  {% endif %}

Which generates this seemingly absurd output:

bob = bob
This is the profile of another user

Why is it so and how to fix it?

UPDATE: Here is the UserProfile model:

class UserProfile(models.Model):
    username =  models.OneToOneField(User)
    name = models.CharField(max_length=30, blank=True)
    city = models.CharField(max_length=30, blank=True)
    canpost=models.BooleanField(default=True)


    User.profile = property(lambda u:UserProfile.objects.get_or_create(username=u)[0])

Upvotes: 0

Views: 48

Answers (1)

Hybrid
Hybrid

Reputation: 7049

Your code is comparing the user username (string) against the profile username (which is an object). You can use profile.username.username instead of profile.username, but the proper convention (which is also more logical), is to rename your Profile model's username field to user, and access the name via profile.user.username.

Upvotes: 2

Related Questions