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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:27:51+00:00 2026-06-17T21:27:51+00:00

python 2.6, with Django 1.3.1 on Redhat 6.3 In Django how would I go

  • 0

python 2.6, with Django 1.3.1 on Redhat 6.3

In Django how would I go about changing the background colour of a table cell depending on it’s value, as in if it is over 10 it’s red, between 7 and 9 it’s orange, below 7 is green etc..

The data is coming from a non django database/model.

I am using a standard template to iterate over the table, but would have no problem using a custom template for this.

I see the following

Link

that deals with changing cell colour but it seems to be based on a concrete value in the cell as opposed to being within a range.

using the following test code for a view

def dashboard(request):
  if request.user.is_authenticated():
      user = request.user.first_name
  else:
      return redirect('/bcpm/login')


  table_headers = ['Colmun1','Column2','Column3']
  table_data = [['test1',2,3],['test2',2,4],['test3',5,5]]
  page_title = 'Dashboard'

  template_dict = {'header_list':table_headers, 'page_title':page_title,
                         'results':table_data,'username':user}
  return render_to_response('dashboard.html',template_dict)enter code here

and the following generic table template:

<table border=1 width=98% style="margin-left:12px;">
        <tr>
            {% for item in header_list %}
            <th>{{ item }}</th>
            {% endfor %}
        </tr>


            {% for row in results %}
            <tr>
                {% for line in row %}
                <td>{{line}}</td>
                {% endfor %}
            </tr>
            {% endfor %}

    </table>

Thanks.


Almost solved;

With the help of brianbuck below i came up with the following,
in the view:

def dashboard(request):
    if request.user.is_authenticated():
        user = request.user.first_name
    else:
       return redirect('/login'

    table_headers = ['Column1','Column2','Column3']
    table_data = [['name','thing',8],['name','thing',5]]
    page_title = 'Dashboard'

    template_dict = {'header_list':table_headers, 'page_title':page_title,
                         'results':table_data,'username':user}
    return render_to_response('dashboard.html',template_dict)

in the template;

<table border=1 width=68% style="margin-left:12px;">
        <tr>
            {% for item in header_list %}
            <th>{{ item }}</th>
            {% endfor %}
        </tr>


            {% for element in results %}
          <tr>
            <td> {{ element.0 }} </td>
            <td> {{ element.1 }} </td>
            {% if element.3 > 7 %} <td class="red"> {{ element.3 }} </td> 
            {% else %} <td class="green"> {{ element.3 }} </td> {% endif %}
          </tr>
            {% endfor %}

    </table>
{% endif %}

I really could not get it to do an {% if or %}
When I tried to set it up to do a

“greater than or equal to 7 or less than or equal to 8”

it would always evaluate to this expression for a number higher than 7, even though the first if statement should be true for anything higher than 9.

I am using Django 1.3 and I think there may be some limitations of the if/else and the multiple evaluations, either way I have it 80% working with two values red/green and that is good enough for the moment.

Thank you all.


Got it to work like this;

{% for element in results %}
            <tr>
            <td> {{ element.0 }} </td>
            <td> {{ element.1 }} </td>
            <td> {{ element.2 }} </td>
            <td> {{ element.3 }} </td>
            {% if element.4 > 8 %} <td class="red"> {{ element.4 }} </td>
            {% else %}{% if element.4 > 8 or element.4 >= 5 %} <td class="orange"> {{ element.4 }} </td>
            {%else %}{% if element.4 < 5 %}<td class="green"> {{ element.4 }} </td>
            {% endif %}{% endif %}{% endif %}

            <td> {{ element.5 }} </td>

This would not be required if you have a version of Django that supports elif or if you add some of the django snippets that are available to extend your django installation.

Hurrah.

  • 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-17T21:27:52+00:00Added an answer on June 17, 2026 at 9:27 pm

    This assumes you have three classes named:

    td.red {
        backgroundColor: red;
    }
    td.orange {
        backgroundColor: orange;
    }
    td.green {
        backgroundColor: green;
    }
    

    …

    Django 1.3 doesn’t have elif so you will probably have to do it a bit more clunky.

    <td class="
        {% if val >= 10 %}red{% endif %}
        {% if val >= 7 or val <= 9 %}orange{% endif %}
        {% if val < 7 %}green{% endif %}">
        {{ val }}
    </td>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django python problem: The select drop down returns the numeric value of the item.
I program Django/Python in emacs, and I would like things like {% comment %}
I'm just moving over from Ruby/Rails development to Python/Django and i'm trying to find
I'm runnig Python Django on Apache2 with ModWSGI and I would like to run
I am from Python-Django background now i am doing my project in PHP. I
I'm just beginning to learn about python/django. I know PHP, but I wanted to
This is my application environment: Redhat 6.0 Apache 2.2 Django 1.3.0 Python 2.6.6 cx_Oracle
I'm using Python/Django, but this is more about the data model and how I
I have a App Engine/Python/Django application which has grown and been modified over the
I have python/django app on Heroku (Cedar stack) and would like to make it

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.