[Django]-Providing current user to all templates in django project

3👍

There’s a template context processor for this: django.core.context_processors.auth. Check out the documentation, you can access the current user and the permissions in the templates.

👤msc

0👍

You can indeed use a custom tag to achieve that.
Note that by default the custom tag has no access to the request object and therefore to the user data. In order to gain access to the request you can define the custom tag with the takes_context attribute and grab the request from the context:

Something like:

@register.inclusion_tag('userdata.html', takes_context = True)
def userdata(context):
    request = context['request']
    user = request.user
    return {'user':user}

0👍

You can just use {{ request.user }} to display the name of the current user, or maybe for example {{ request.user.is_authenticated }} to find out if it’s an AnonymousUser or not.

Leave a comment