94

I'm storing some additional per-user information using the AUTH_PROFILE_MODULE.

We can access the user in a Django template using {{ request.user }} but how do we access fields in the profile since the profile is only accessible via a function user.get_profile() ?

Is it really required to explicitly pass the profile into the template every time?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Swaroop C H
  • 16,902
  • 10
  • 43
  • 50

6 Answers6

141

Use {{ request.user.get_profile.whatever }}. Django's templating language automatically calls things that are callable - in this case, the .get_profile() method.

AdamKG
  • 13,678
  • 3
  • 38
  • 46
  • 3
    See http://docs.djangoproject.com/en/dev/topics/templates/#variables The rules are very cool. – S.Lott Jan 07 '09 at 22:14
  • 3
    This is deprecated in Django 1.5 and up and does not work in Django 1.7 and up. See Sacha Rau's answer for how to do this in modern Django. – MichielB May 17 '17 at 13:18
28

Not sure why it's different for me, but I need to use {{user}} rather than {{request.user}}.

Rachel
  • 2,858
  • 2
  • 26
  • 30
  • 5
    The docs say (http://docs.djangoproject.com/en/dev/topics/auth/#authentication-data-in-templates) that you can access the user simply by {{user}}, as you say. – Xiong Chiamiov Feb 02 '10 at 02:48
  • 2
    The real reason for using {{user}} and not {{request.user}} is because you do not include the template context processor for request. [https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request](https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request) – knightZeRo Mar 02 '14 at 21:18
  • 6
    The link is slightly different for Django 1.10, but idea is the same, the `django.template.context_processors.request` inserts the `request` in template, while the `django.contrib.auth.context_processors.auth` inserts `user` in context. https://docs.djangoproject.com/en/1.10/ref/templates/api/#django-template-context-processors-request – Andrei-Niculae Petre Aug 09 '16 at 12:55
8

Yes it is possible to access profile from template using request.user.get_profile

However there is a small caveat: not all users will have profiles, which was in my case with admin users. So calling directly {{ request.user.get_profile.whatever }} from the template will cause an error in such cases.

If you are sure all your users always have profiles it is safe to call from template, otherwise call get_profile() from within try-except block in your view and pass it to the template.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Sergey Golovchenko
  • 18,203
  • 15
  • 55
  • 72
  • This answer is misleading - I haven't tested `{{ request.user.get_profile.whatever }}`, but `{{ request.user.get_profile }}` will work and can be used to check whether a profile exists or not in current versions. See http://docs.djangoproject.com/en/dev/ref/templates/api/#how-invalid-variables-are-handled . There is a note that that will change in the development version, though, see the paragraph above the one I linked to. – ralokt Jan 16 '11 at 09:45
  • @tkolar, what exactly is misleading here? if user doesn't have a profile and you are calling get_profile.something that will cause an error. – Sergey Golovchenko Feb 22 '11 at 18:13
5

If it helps anyone, I used the followings in my template:

Username: {{ user.username }}

User Full name: {{ user.get_full_name }}

User Group: {{ user.groups.all.0 }}

Email: {{ user.email }}

Session Started at: {{ user.last_login }}

A sample result is like this:

User: auditor ezio

User Group: auditGroup

Username: testUser03

Email: testuser03@auditor.com

Session Started at- April 16, 2018, 9:38 p.m.

Thanks :)

Community
  • 1
  • 1
Mohammad
  • 657
  • 1
  • 7
  • 18
4

If you are using Django > 1.5 you can no longer use get_profile.

If you have a legacy app, you should remove AUTH_PROFILE_MODULE = 'myapp.profile' from your settings.py.

If you use models.OneToOneField(User) in your Profile class, you can simply use

{{ request.user.profile.whatever }}

in your Django template

MichielB
  • 4,181
  • 1
  • 30
  • 39
Sascha Rau
  • 357
  • 3
  • 12
1

Working !

In your profile model provide related_name

user = models.OneToOneField(AUTH_USER_MODEL, related_name="user_profile", on_delete=models.CASCADE)

Then in template use. Here company_name is field in profile table

{{ request.user.user_profile.company_name }}
Nids Barthwal
  • 2,205
  • 20
  • 12