【问题标题】:Prevent django widthratio calculating more than 100 %防止 django widthratio 计算超过 100 %
【发布时间】:2014-11-30 15:34:07
【问题描述】:

我正在尝试将具有多个百分比值 (see docs example) 的 bootstrap 3 进度条放在一起。

所有值的组合值不能超过 100%,但是 django 正在四舍五入,我得到的值是 101%

#max_width = 100
#total.count = 17

#a.count = 11 (65%)
#b.count = 2 (12%)
#c.count = 4 (24%)


<div class="progress">   
  <div class="progress-bar" style="width: {% widthratio a.count total.count max_width %}%">
    {{ a.count }} ({% widthratio a.count total.count max_width %}%)
  </div>

  <div class="progress-bar" style="width: {% widthratio b.count total.count max_width %}%">
    {{ b.count }} ({% widthratio b.count total.count max_width %}%)
  </div>

  <div class="progress-bar" style="width: {% widthratio c.count total.count max_width %}%">
    {{ c.count }} ({% widthratio c.count total.count max_width %}%)
  </div>

</div>   

输出

<div class="progress">
      <div class="progress-bar" style="width: 65%">
        11 (65%)
      </div>
      <div class="progress-bar" style="width: 12%">
        2 (12%)
      </div>
      <div class="progress-bar" style="width: 24%">
        4 (24%)
      </div>
</div>

如您所见,所有百分比加起来为 101%,因此最后一个值不会显示在进度条中。有没有办法解决这个问题?

【问题讨论】:

    标签: django twitter-bootstrap-3 django-templates


    【解决方案1】:

    我发现了这个,这并没有真正的帮助。我正在研究同样的问题。
    https://code.djangoproject.com/ticket/12026

    不确定这是否是最好的方法,但是:

    @register.simple_tag
    def percentage(value, total):
        return float(value)/float(total) * 100
    

    然后在模板中: style="width: {% percent group.count total.count %}%;"

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多