Django Templates and Conditional CSS classes

1,826 views
Skip to first unread message

treyd

unread,
Nov 10, 2017, 3:40:53 PM11/10/17
to Django users
Hello,

I am trying to render a model field in a django template, and, based on the value of the field, I want to specify a different CSS class.  Right now, my solution is this:

<span class="label {% if myobject.state == 'Scheduled' %}label-default
                   {% elif myobject.state == 'Provisioning' %}label-primary
                   {% elif myobject.state == 'Active' %}label-success
                   {% elif myobject.state == 'Deleting' %}label-primary
                   {% elif myobject.state == 'Ended' %}label-info
                   {% endif %}"
>{{ myobject.state }}</span>

This works, but seems like a really ugly solution and a bunch of logic in the template.  Is there a better way of doing this?  My thought was to put the class names as another field in the model, but that seems like it would violate the MVC (or MVT) separation. Any ideas?

Thanks in advance,
treyd

Adam Simon

unread,
Nov 10, 2017, 3:51:09 PM11/10/17
to django...@googlegroups.com

Are you open to using JavaScript in the front end? It would make it really easy

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6a3adb62-f3d7-4cc2-8c97-add34cf757ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--

Adam F. Simon, PhD
Calabasas, CA.

cell:      818-425-3719
home:   818-880-8405

Feel free to link w/ me: LinkedIn


treyd

unread,
Nov 10, 2017, 4:36:19 PM11/10/17
to Django users
At some point I plan on figuring out how to do more intelligent front-end in-browser stuff (Angular, etc) which yeah would definitely help me here but I was wondering if there was a Django-only way of doing this.

Adam Simon

unread,
Nov 10, 2017, 5:02:25 PM11/10/17
to django...@googlegroups.com

 You can pass the class from either the model or the view into the template. For example, you can assign a class to a button with a variable:

<button class=“{{ class }}”><|button>

And then the class would have to be defined in your style somewhere


For more options, visit https://groups.google.com/d/optout.

Vijay Khemlani

unread,
Nov 10, 2017, 5:45:02 PM11/10/17
to django...@googlegroups.com
You can also add a "state_css_class" (or something) method to your object class and just call

<span class="label {{ myobject.state_css_class  }}">...</span>

the method should be quite simple

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
--
--

Adam F. Simon, PhD
Calabasas, CA.

cell:      818-425-3719
home:   818-880-8405

Feel free to link w/ me: LinkedIn


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

treyd

unread,
Nov 10, 2017, 5:50:38 PM11/10/17
to Django users
Thanks, all, for the info.  Looks like the best way to clean this up is to make the model have some knowledge of the CSS class I want to use and make a method to get it.  I like that, but I was unsure about sticking view information into the model.  Probably the best solution is javascript logic in the browser, which I'll get to.
--
--

Adam F. Simon, PhD
Calabasas, CA.

cell:      818-425-3719
home:   818-880-8405

Feel free to link w/ me: LinkedIn


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Omar Helal

unread,
Nov 11, 2017, 10:18:25 AM11/11/17
to Django users
Hi treyd, 
I think idea of updating the model and storing it in the db is a bit overkill, however you could create an @propery on the model that will do that logic for you and return the css class for you.
e.g.
@property
def css_class(self):
  if self.state is SUCHANDSUCH:
     return "label-soandso"

and so in the template you can do what Vijay recommended but without storing anything you don't need in the database.

Good luck with going Angular, it plays well with Django! 

treyd

unread,
Nov 11, 2017, 2:49:30 PM11/11/17
to Django users
Omar (and everybody else who responded),

Thanks for that ideas and putting a method on the model to return the appropriate CSS class based on the state.  I think that would be the cleanest solution if I am going to bite the bullet and put this template-specific logic on the model.

It got me thinking, though, and it might be a better place to handle this in the view.  I tried that and I got it to work by extending DetailView into a custom view modified the context before rendering the template.  This removed the template logic, but I ran into another issue when I am trying to access other models associated with the model I'm looking at in the DetailView and handling their state label styling, so I scrapped that approach.

I did some more research and the solution I finalized on was a custom template filter that would read the state and convert it to the appropriate CSS class, e,g,:

<span class="label {{ myobject.state|state_css_class }}">{{ myobject.state }}</span>

To do this, I followed the Django Custom Template Tags How-To and came up with this:
from django import template

register = template.Library()


@register.filter
def state_css_class(value):
   
"""returns appropriate bootstrap label class for states"""
    statemap
= {
       
'Scheduled': 'label-default',
       
'Provisioning': 'label-primary',
       
'Active': 'label-success',
       
'Deleting': 'label-primary',
       
'Ended': 'label-info',
       
'Unprovisioned': 'label-default',
       
'Building': 'label-primary',
       
'Deleted': 'label-primary',
       
'Error': 'label-danger'
   
}
   
try:
       
return statemap[value]
   
except KeyError:
       
return 'label-default'


I like this solution the best because it keeps template logic in/near the template layer, I can add new state renderings easily, and I can extend it to handle states for multiple model types (i have more than one stateful model in my project).

I hope someone else finds this useful!  Thanks again all for the discussion.

PASCUAL Eric

unread,
Nov 11, 2017, 7:23:54 PM11/11/17
to django...@googlegroups.com

Hi,


Since it is static (i.e. never modified), the CSS classes dictionary (statemap) could be extracted from the function and promoted as a module global variable, to avoid it being reconstructed each time the filter is invoked.


This will not cut the processing time in half, but small streams make big rivers :)


Eric

From: django...@googlegroups.com <django...@googlegroups.com> on behalf of treyd <tr...@treyd.us>
Sent: Saturday, November 11, 2017 20:49
To: Django users
Subject: Re: Django Templates and Conditional CSS classes
 
Reply all
Reply to author
Forward
0 new messages