How to access the user profile in a Django template?

PythonDjangoDjango Templates

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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: [email protected] > > Session Started at- April 16, 2018, 9:38 p.m.

Thanks :)

Solution 5 - Python

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

Solution 6 - Python

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 }}


                           

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSwaroop C HView Question on Stackoverflow
Solution 1 - PythonAdamKGView Answer on Stackoverflow
Solution 2 - PythonRachelView Answer on Stackoverflow
Solution 3 - PythonSergey GolovchenkoView Answer on Stackoverflow
Solution 4 - PythonMohammadView Answer on Stackoverflow
Solution 5 - PythonSascha RauView Answer on Stackoverflow
Solution 6 - PythonNids BarthwalView Answer on Stackoverflow