0

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.

Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
Sandip Nath
  • 622
  • 2
  • 8
  • 18

1 Answers1

1

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 }}
Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
  • Ok got this. but what should I write in the urls.py file? I wrote : path('my-profile//', views.ProfileView.as_view(), name='my_profile'). Is this ok. Again the link to go the profile page is written as: Edit profile. Is this ok too. Please advise. – Sandip Nath Jun 11 '20 at 16:37
  • @SandipNath you should use the name of the url path and include its namespace if necessary `{% url 'my_profile' request.user.pk %}`, if there is some confusion, check [the docs](https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#url) – Bernardo Duarte Jun 11 '20 at 16:56
  • @SandipNath Glad I could help! If there's something else I can help just ping! Consider upvoting if you think it's a good answer ;D – Bernardo Duarte Jun 11 '20 at 17:30
  • Many many thanks Bernardo. But a small problem still lies there and that is by writing request.user.picture in the template file, the filename of the picture is displayed, not the picture itself. How the picture, not it's name can be shown? – Sandip Nath Jun 11 '20 at 18:35
  • @SandipNath How are you currently displaying the picture? – Bernardo Duarte Jun 11 '20 at 19:48
  • @SandipNath I believe your problem could be solved by an already answered question here on so check it out [here](https://stackoverflow.com/a/52452722/7395911) – Bernardo Duarte Jun 11 '20 at 21:18