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?
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.
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.
Use {{ request.user.get_profile.whatever }}
. Django's templating language automatically calls things that are callable - in this case, the .get_profile()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With