70

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

1

5 Answers 5

133

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

46
{% 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',
    ...
)
0
40

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

1
  • I was also intentionally going with {{ user.username }}, but as @user mentioned in the accepted answer, it has some drawbacks and {{ user.get_username }} should be preferred using the user instance.
    – Advena
    May 18, 2020 at 10:48
2

Firstly, First, if your fields changed their name, you must overwrite the functions (get_full_name(), get_short_name(), etc), in this way:

def get_full_name(self):
    return self.names + ' ' + self.lastnames

def get_short_name(self):
    return self.names

In the template, you can show it this way

{% if user.is_authenticated %}
<strong>{{ user.get_short_name }}</strong>
{% endif %}

these are the methods in authentication https://docs.djangoproject.com/es/2.1/topics/auth/customizing/

0

The following is a full working solution that takes also translations into consideration:

template.html:

{% blocktrans %}Welcome {{ USER_NAME }}!{% endblocktrans %}

context_processors.py:

def template_constants(request):
    return {
        'USER_NAME': '' if request.user.is_anonymous else request.user.first_name,
        # other values here...
    }

Remind to set your custom context_processors correctly in the settings.py:

TEMPLATES = [
    {
        # ...
        'OPTIONS': {
            'context_processors': [
                # ...
                'your_app.context_processors.template_constants',
            ],
        },
    },
]

And this is what you get in the django.po:

#: templates/home.html:11
#, python-format
msgid "Hi %(USER_NAME)s!"
msgstr "..."

A good practice is to keep the logic outside the templates: for this purpose you can easily customize the username displayed directly in the context_processors.py.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.