Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:51:46+00:00 2026-06-03T06:51:46+00:00

In every page ( base.html ), I want to check whether request.user has an

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:51:47+00:00Added an answer on June 3, 2026 at 6:51 am

    You can add method is_admin to your UserProfile model moving business logic to models.

    Note that construction like

    {% if user.profile.user_types.all %}
        {% for user_type in user.profile.user_types.all %}
        ...
        {% endfor %}
    {% endif %}
    

    hits 2 sql query to your db. But with template tag reduces them to 1 hit.

    {% with types=user.profile.user_types.all %}
    {% if types %}
        {% for user_type in types %}
        ...
        {% endfor %}
    {% endif %}
    {% endwith %}
    

    Actually the best place for this is in models. But you should learn what django offers for your purpose (contrib.auth, permissions, user groups). Probably you reinvent the wheel.

    Then condition {% if user_type.name == "ad" %} shouldn’t be hard coded in your python code (especially in templates).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thus for used base class for some commom reusable methods in every page of
Suppose this is what we want in every page, we'd create base_template.html <title>{% block
I have a pretty large site and every page is built from several included
I want to run the following jquery code on every page in my website.
I need to add two lines of html to every page on a large
On my site I need to request a date/time every page reload. The reason
At the moment, I have a base HTML template file. When ever I want
My current project has a small user base and nearly all clients will be
How can I display content side-wide (on every page) based on if the user
Supposing I need to print the logged user on every page of my app.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.