djangodjango-templates

django applying a style class based on a conditional


I am displaying a list of images. If the user has uploaded an image, I want to keep its opacity 0.5 and in the list of images, the images uploaded by others should have full opacity. I have done it as follows, is there a better way to do it??

{% if request.user == obj.shared_by %}
       <div class="item-image" style="opacity:0.5;filter:alpha(opacity=50);"> 
          {% else  %}
          <div class="item-image"> 
          {% endif %}
        ......Some code here....
       </div>

Thanks!


Solution

  • I normally go for:

    <div class="item-image{% if foo %} own-image{% endif %}">...</div>
    

    but switching out the entire div tag may be more readable.

    Either way I'd do the styling with another class, not with inline css.