django - 为什么 widthratio(乘法)在我的模板中不起作用?

标签 django python-3.x templates multiplication

我正在使用 Django 2.0 和 Python 3.7。我读过我可以使用 widthratio 在模板中进行乘法 -- multiplication in django template without using manually created template tag .但是,在我的模板中,我尝试这样做无济于事。我想

 {% if widthratio articlestat.score 1 TRENDING_PCT_FLOOR >= articlestat.weighted_score %}style="font-weight:bold;"{% endif %}

当我的模板用这段代码执行时,它给出了错误......
Unused 'articlestat.score' at end of if expression.

我希望我的 if 表达式说明“articlestat.score”和“TRENDING_PCT_FLOOR”的倍数是否大于“articlestat.weighted_score”,打印出来,但我似乎无法弄清楚如何做到这一点。

最佳答案

您不能在 if 中使用模板标签像这样的语句条件。您可以做的是首先分配 widthratio 的输出到模板变量,然后在您的 if 中进行比较陈述:

{% widthratio articlestat.score 1 TRENDING_PCT_FLOOR as ratio %}
{% if ratio >= articlestat.weighted_score %}style="font-weight:bold;"{% endif %}

关于django - 为什么 widthratio(乘法)在我的模板中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60381983/

相关文章:

python - CSRF验证失败。请求被中止。 (禁止(403)) Django

python - 将值附加到 TTK 组合框 ['values' ] 而不重新加载组合框

python-3.x - 为什么 Python 不接受以 0 开头的整数作为有效代码?

python-3.x - 错误没有名为 "PyMySQL"Windows 10 的模块

c++ - 通过可变参数模板传递右值引用时出现编译器错误

c++ - 如何使用模板处理多类型变量?

django - 使用 Django Rest Framework 序列化通用关系,并提供写入支持

Django 中的 Java 应用程序上下文

python - 如何使用当前登录用户作为 Django DetailView 的 PK?

c++ - C++ 中的编译时数组选择