<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>
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/360d495a-6715-40a6-ae25-4ee5aed7e3cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/360d495a-6715-40a6-ae25-4ee5aed7e3cc%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
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAH-jdcwVhxnbn9uRr1C7kR4SP6mB9CN-rAzRU28tCMLZ64bosw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/360d495a-6715-40a6-ae25-4ee5aed7e3cc%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
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.
<span class="label {{ myobject.state|state_css_class }}">{{ myobject.state }}</span>
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'
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 :)