0

Is there a way to get profile information from a userena profile in a template? Is there some sort of variable automatically passed?

E.g: I can get the django user variable like this:

{% if user.is_authenticated %}
   Hey {{ user.username }}.
{% endif %}

since this is passed with each request.

Is there a built in way to do the same with a userena profile without passing a profile with each view? Ideally, I'd like to do sth. like this:

{% if user.is_authenticated %}
    {% if user.profile.likes_cookies %}
       Hey cookiemonster
    {% endif %}
{% endif %}
Jay
  • 2,519
  • 5
  • 25
  • 42

1 Answers1

2

You can access the user's profile directly in template.

Say that I have defined my userena profile as follows:

class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=_("user"),
                                related_name="my_profile")
    # Example of adding additional fields below.
    likes_cookies = models.BooleanField(_("likes cookies"))

The related_name on the OneToOneField with the User class is the important part here, because it is how you will get access to the userena profile.

Now in the templates you will be able to access the profile as follows:

{% if user.is_authenticated %}
    {% if user.my_profile.likes_cookies %}
       Hey cookiemonster
    {% endif %}
{% endif %}
jondykeman
  • 6,172
  • 3
  • 23
  • 22