【问题标题】:django applying a style class based on a conditionaldjango 应用基于条件的样式类
【发布时间】:2013-01-02 23:30:43
【问题描述】:

我正在显示图像列表。 如果用户上传了一张图片,我想保持它的不透明度 0.5 并且在图片列表中,其他人上传的图片应该是完全不透明度的。 我做了如下,有没有更好的方法呢??

{% 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>

谢谢!

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    我通常会选择:

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

    但换掉整个 div 标签可能更具可读性。

    无论哪种方式,我都会使用另一个类进行样式设置,而不是使用内联 css。

    【讨论】:

    • 为什么我不能这样做&lt;div class="item-image{% for foo in foos %} {% if foo %} own-image{% endif %} {% endfor %}"&gt;...&lt;/div&gt; 为什么我不能在元素属性中使用 for 循环?
    • 这应该是一个新问题,但应该可以。模板语言不知道也不关心周围 html 的语义
    【解决方案2】:

    我已经通过这种方式在 if 条件下添加了类....

    &lt;li class="nav-item {% if app_url == '/' %} active{% endif %}"&gt;

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 2015-09-27
      • 2022-11-27
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      相关资源
      最近更新 更多