I have defined a user model inheriting AbstractUser
from django.auth.models
. How can I refer to each individual fields of that customized user model? Say if I want to refer to date of birth of the customised user what should I write? I need to show the user profile, so in show_profile.html
file, I wrote :
first name = {{ settings.AUTH_USER_MODEL.first_name }}
date 0f birth = {{ settings.AUTH_USER_MODEL.dob }}
But it didn't work. Any idea? Moreover, my url route looks like this:
path('my-profile/', views.ProfileView.as_view(), name='my_profile'),
The concerned line in base.html is:
<a class="dropdown-item" href="{% url 'my_profile' %}">Edit profile</a>
The concerned lines in views.py are:
class ProfileView(LoginRequiredMixin, TemplateView):
template_name = 'show_profile.html'
Please tell where I have done wrong.
You shouldn't access current user information like that, because it is wrong. What you're actually getting with settings.AUTH_USER_MODEL
is the model class not the object instance of the current user. You shuld access it through the request
object via Django's context processors.
On your settings.py
you should have something like this:
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.request',
...
],
},
},
]
Make sure you have django.template.context_processors.request
there, and then to access the current user information you just need to use like this:
first name = {{ request.user.first_name }}
date 0f birth = {{ request.user.dob }}