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 3354618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:17:42+00:00 2026-05-18T02:17:42+00:00

I’m trying to display an HTML table of values with about 20 columns where

  • 0

I’m trying to display an HTML table of values with about 20 columns where say staff users see one subset of columns, and non-staff users see another subset of columns. I may want to define further types of users later. Now right now I have three static header rows so the template looks like

<table>
<tr>
  <th>Col A</th>
  {% if user.is_staff %}<th>Col B</th>{% endif %}
  ...
  {% if not user.is_staff %}<th>Col K</th>{% endif %}
</tr>
<tr>
  <td>Col A second header</td>
  {% if user.is_staff %}<td>Col B second header</td>{% endif %}
  ...
  {% if not user.is_staff %}<td>Col K second header</td>{% endif %}</tr>
<tr><td>Col A third header</td>  ...  </tr>

{% for obj in object_list %}
<tr>
  <td>{{ obj.col_a }}</td>
  {% if user.is_staff %}<td>{{ obj.col_b }}</td>{% endif %}
  ...
  {% if not user.is_staff %}<td>{{ obj.col_k }}</td>{% endif %}
</tr>
{% endfor %}</table>

However, I find non-DRY as every time, if I want to change if a user-type can see a column, I have to change it in 4 places. Or if I want to define multiple different classes of users, I’d have to have complicated if statements. I’d prefer something like

 {% if show_col_a %}<td>{{obj.col_a }}</td>{{% endif %}

Where I can define at the top of the template (or possibly in the view) that user.is_staff can see show_col_a. Is something like this possible? I’m using a generic view (object_list). Maybe modify all users to have attributes user.show_col_a somehow and do {% if user.show_col_a %}? I’m not sure how to add boolean attributes to users.

EDIT: May want multiple users with custom views (e.g., staff_view; admin_view, unprivileged, etc.), so if statements would get unwieldy. A cell’s contents is typically more complicated than {{ obj.col_b }}; tried simplifying problem to get to the point. E.g.:

<td>{% if obj.custom_address %}
  {{ obj.custom_address.webprint|safe }}
{% else %}
  {{ obj.business.address.webprint|safe }}
{% endif %}</td>

Also while multiple templates would work with a simple switch like:

{% if user.is_staff %}
   {% include "template_staff.html" %}
{% else %}{% if user.is_admin %}
   {% include "template_admin.html" %}
{% else %}
   {% include "template_other.html" %}
{% endif %}
{% endif %}

I find its not DRY at all; e.g., every edit to a template has to be replicated in three template. I guess I could make a script that read generates the three templates from some super_template outside of django but its getting very inelegant.

  • 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-05-18T02:17:42+00:00Added an answer on May 18, 2026 at 2:17 am

    This depends a lot on what view you have and templates.

    Ways to do:

    • make a public template and staff template and add a simple method to change the templates on the fly for the views.
    • make a template tag:

      {% is_staff myvar %}

    tag code:

    class IsStaffNode(Node):
        def __init__(self, var):
            self.var = var
    
        def render(self, context):
            if context['user'].is_staff():
                return var.resolve(context)
            return ""
    
    
    @register.tag
    def is_staff(parser, token):
        var = parser.compile_filter(token.split_contents()[1])
        return IsStaffNode(var)
    

    Homework: make it a block tag to include the td’s so that it’s shown either all or none.

    {% isstaff myvar %}<td>{{ myvar }}</td>{% endisstaff %}
    

    This way is more labor intensive than 2 different templates, but if you want to try, manipulating the context (or creating a separate context for the block only) might be useful.

    • Make a context processor that would fill the context with some variables if the user is staff, or not if not.
    • Make a tag that would include the template (inherit from IncludeNode) and manipulate the context.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters

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.