Django conditional formatting method

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 the color of the rows depends on the value of this date field. For example, records that correspond to today's date, I want to be yellow, records that exist until today, I want green and one after I want red.

Somewhere in Django you will need to do this comparison by comparing the current date with the date of recording.

I see three different places that could be compared:

  • Add a method to my model, for example status (), which returns either "past", "present", "future", and then uses this in the template to color the strings.
  • In the view, instead of returning a set of queries to the template, pre-process the list and compare each record, build a new dict containing the "past", "present" and "future" values ​​that will be used in the template
  • Create a new template tag that performs the comparison.

Which of these methods is the correct way for Django? It seems like conditional formatting is something that would happen quite often, and since you cannot make arbitrary comparisons in a template, a different solution is needed.

The same applies to simpler formatting rules, for example, if I wanted to display a list of student grades, and I would like to make them more than 80% green, and those below 30% red.

+6
python django formatting django-templates
source share
4 answers

I am a big fan of placing business logic in the view function and ALL presentations in templates / CSS.

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

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

Now you can change the rules without breaking the pattern. You can change CSS without touching HTML or Python code.

 {% for date,state in the_date_list %} <span class="{{state}}">date</span> {% endfor %} 
+8
source share

I had a very similar requirement; since this is pretty much related to business logic, I added a model method for managing the kind of information that will be used in the template:

 {% if not bug.within_due_date %}bgcolor="OrangeRed"{% endif %} 

It can also be obtained using a template or filter; but in my case, I felt that the best place for logic was inside the model; I would suggest you analyze it in the same way.

+3
source share

Since you are performing a static comparison (no queries), you should choose the most severe and easiest option to implement. In this case, I would choose option 4 by creating a template filter. Then you can make a value | filter to get the class you need to set the background color. Template filters are actually a little simpler than template tags to implement.

+1
source share

you can also take a look at the Django link for inline formatting and filtering tags. It probably has what you are looking for and more, and it’s a good bookmark link.

You can look at it here .

0
source share

All Articles