Get user information in the Django template (admin)

38 Views Asked by At

I am trying to access information related to the present logged-in user in the Django admin custom template change_list_results.html for a specific model. But I am not able to access any user information in the template. We are using Django 4.0.4. I am over-riding an admin template (change_list_results.html) and trying to get user information in the template(either username or email).

I have tried to use the below variables (suggested in some other threads), but none of them work.

{{ user }}
{{ user.username }}
{{ request.user }}
{{ request.user.username }}

Below is how the template context_processors being set in the settings.py. Some people suggested that django.contrib.auth.context_processors.auth in this should give access to user object. But I do not get the user object in template:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ], 
        },

    },
]

I tried to add a context variable by adding below block to the model, but even this did not work. Referencing {{username}} after this just results in empty value

    def changelist_view(self, request):
        return super().changelist_view(request, extra_context={"username": request.user.username})

Please help with how I can get information related to user within the template.

Thanks!

0

There are 0 best solutions below