【发布时间】:2019-07-24 02:09:39
【问题描述】:
我正在尝试在我的应用程序中创建评分(星)用户界面。我所有的“收视率”都是float。我想将该评级四舍五入以使其成为整数并显示那么多星。我似乎无法弄清楚如何让 jinja 喜欢它。
示例评分:3.0、2.3、5.0、4.6 等...
失败,因为TypeError: 'float' object cannot be interpreted as an integer
{% if book.average_score %}
{% for s in range(book.average_score) %}
<i class="fas fa-star"></i>
{% endfor %}
{% endif %}
我以为我可以使用math:
{% if book.average_score %}
{% for s in range(math.ceil(book.average_score)) %}
<i class="fas fa-star"></i>
{% endfor %}
{% endif %}
但是,这会导致jinja2.exceptions.UndefinedError: 'math' is undefined。我假设这是因为我正在使用 Flask 并且模板不知道 math 库。
然后我在玩round:
{% if book.average_score %}
{% for s in range(round(book.average_score)) %}
<i class="fas fa-star"></i>
{% endfor %}
{% endif %}
但后来我得到了jinja2.exceptions.UndefinedError: 'round' is undefined
我在the round documentation 之后使用round 做了更多变体,但没有成功。我知道在 Angular 中,你有 pipes 对这类事情真的很有帮助。 jinja 有类似的东西吗?还是我在这里离题了?
This SOF 线程似乎是我能找到的最接近我要解决的问题的东西。然而,似乎并没有让我走得更远。
【问题讨论】: