Django: detect admin login in view or template

In templates:

{% if user.is_superuser %}
    <p>Hello, admin.</p>
{% else %}
    <p>Hello, ordinary visitor.</p>
{% endif %}

In views:

if request.user.is_superuser:
    # Hello, admin.
else:
    # Hello, ordinary visitor.

Depending on your needs, is_staff might be a better fit than is_superuser. You can read about the difference here.

Leave a Comment