djangodjango-authenticationdjango-registration

Get user information in django templates


What's the best way to get user information from a django template?

For example, if I just want to:

  1. If the user is logged in, display "Welcome [username]"
  2. Otherwise, display the login button.

I'm using django-registration/authentication


Solution

  • 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:

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