我正在使用 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/