Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the user profile in a Django template?

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?

like image 536
Swaroop C H Avatar asked Jan 07 '09 21:01

Swaroop C H


People also ask

How can I see profile in Django?

Open users app urls.py and add the route for profile view. Create the template for the view inside the users app template directory. We will modify this template later to display profile of the user, but first there are a couple of things we need to do.

How do I change user details in Django?

Add "accounts" at the bottom of "INSTALLED_APPS". And add the "AUTH_PROFILE_MODULE" config at the bottom of the entire file. We will add "accounts" app in settings.py and use the "AUTH_USER_MODEL" config to tell Django to use our another model. And our another model will UserProfile.


1 Answers

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

like image 101
AdamKG Avatar answered Sep 18 '22 19:09

AdamKG