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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:31:34+00:00 2026-05-12T00:31:34+00:00

What is the correct way to do conditional formatting in Django? I have a

  • 0

What is the correct way to do conditional formatting in Django?

I have a model that contains a date field, and I would like to display a list of records, but colour the rows depending on the value of that date field. For example, records that match today’s date I want to be yellow, records that is before today I want green and ones after I want red.

Somewhere in Django you will need to do that comparison, comparing the current date with the date in the record.

I can see three different places that comparison could be done:

  1. Add a method to my model, for example, status(), that returns either ‘past’, ‘present’, ‘future’ and then use that in the template to colour the rows.
  2. In the view instead of returning a queryset to the template, pre-process the list and compare each record, build a new dict containing the ‘past’, ‘present’ and ‘future’ values to be used in the template
  3. Create a new template tag that does the comparison.

Which of these methods is the correct Django way of doing it? It sounds like conditional formating is something that would come up quite often, and since you can’t do arbitrary comparisons in the template some other solution is needed.

The same would apply for simpler formatting rules, for example, if I wanted to display a list of student grades, and I wanted to make the ones higher than 80% green and the ones below 30% red.

  • 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-12T00:31:34+00:00Added an answer on May 12, 2026 at 12:31 am

    I’m a big fan of putting ALL “business” logic in the view function and ALL presentation in the templates/CSS.

    Option 1 is ideal. You return a list of pairs: ( date, state ), where the state is the class name (“past”, “present”, “future”).

    Your template then uses the state information as the class for a <span>. Your CSS then provides the color coding for that span.

    You are now free to change the rules without breaking the template. You can change the CSS without touching HTML or Python code.

    {% for date,state in the_date_list %}
        <span class="{{state}}">date</span>
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 130k
  • Answers 130k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Membership does automatically use UTC as described in this article:… May 12, 2026 at 6:07 am
  • Editorial Team
    Editorial Team added an answer If your goal is to take advantage of database indexes,… May 12, 2026 at 6:07 am
  • Editorial Team
    Editorial Team added an answer The server you get the content from transfers the data… May 12, 2026 at 6:07 am

Related Questions

I am beginning to believe that unit testing high level, well-written code, which requires
I have a table called Basic , start_time is one field with type :VARCHAR(5),
I wanted to have more than one controller and view for same object/model in
This is a followup to How can I get around a ‘die’ call in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.