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.

On Friday, November 10, 2017 at 5:45:02 PM UTC-5, Vijay Khemlani wrote:
>
> 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
>
> On Fri, Nov 10, 2017 at 7:01 PM, Adam Simon <add.si...@gmail.com 
> <javascript:>> wrote:
>
>>
>>  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
>>
>> On Fri, Nov 10, 2017 at 1:37 PM treyd <tr...@treyd.us <javascript:>> 
>> wrote:
>>
>>> 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.
>>>
>>>
>>> On Friday, November 10, 2017 at 3:51:09 PM UTC-5, Adam wrote:
>>>
>>>>
>>>> Are you open to using JavaScript in the front end? It would make it 
>>>> really easy
>>>>
>>> On Fri, Nov 10, 2017 at 12:40 PM treyd <tr...@treyd.us> wrote:
>>>>
>>> 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
>>>>>
>>>>> -- 
>>>>> 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
>>>>>  
>>>>> <https://groups.google.com/d/msgid/django-users/6a3adb62-f3d7-4cc2-8c97-add34cf757ce%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> 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 <javascript:>.
>>> To post to this group, send email to django...@googlegroups.com 
>>> <javascript:>.
>>> 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
>>>  
>>> <https://groups.google.com/d/msgid/django-users/360d495a-6715-40a6-ae25-4ee5aed7e3cc%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> 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 <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <javascript:>.
>> 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
>>  
>> <https://groups.google.com/d/msgid/django-users/CAH-jdcwVhxnbn9uRr1C7kR4SP6mB9CN-rAzRU28tCMLZ64bosw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/68d7b70e-5c34-4618-a5ca-9f0e0757101c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to