In every page (base.html
), I want to check whether request.user
has an administrator role from my class UserTypes
and show the admin link. Currently I do something like this:
{% if user.profile.user_types.all %}
{% for user_type in user.profile.user_types.all %}
{% if user_type.name == "ad" %}
<li>
<a href="{% url admin:index %}" class="round button dark ic-settings image-left">Admin</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
user.profile
is simply going from Django's User
to my UserProfile
.
But this seems a little verbose and clunky. Is there a simpler way? Maybe I should be writing my own custom context processor and pass a variable like is_admin
or something, but I never wrote a custom context processor before...