Get user information in django templates

{% if request.user.is_authenticated %}Welcome '{{ request.user.username }}'
{% else %}<a href="{% url django.contrib.auth.login %}">Login</a>{% endif %}

and make sure you have the request template context processor installed in your settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.request',
    ...
)

An alternate method for current Django versions:

{% if user.is_authenticated %}
    <p>Welcome, {{ user.get_username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}


Note:

  • Use request.user.get_username() in views & user.get_username in templates. Preferred over referring username attribute directly. Source
  • This template context variable is available if a RequestContext is used.
  • django.contrib.auth.context_processors.auth is enabled by default & contains the variable user
  • You do NOT need to enable django.core.context_processors.request template context processor.

Source : https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates


As per the question title, the following may come handy to someone. used the followings in my template:

Username: {{ user.username }}

User Full name: {{ user.get_full_name }}

User Group: {{ user.groups.all.0 }}

Email: {{ user.email }}

Session Started at: {{ user.last_login }}

Thanks :)