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

Dav*_*ave 5 django templates multiplication python-3.x

我正在使用 Django 2.0 和 Python 3.7。我读过我可以通过使用 widthratio 在模板中进行乘法——django 模板中的乘法而不使用手动创建的模板标签。但是,在我的模板中,我尝试这样做无济于事。我想

 {% if widthratio articlestat.score 1 TRENDING_PCT_FLOOR >= articlestat.weighted_score %}style="font-weight:bold;"{% endif %}
Run Code Online (Sandbox Code Playgroud)

当我的模板用这段代码执行时,它给出了错误......

Unused 'articlestat.score' at end of if expression.
Run Code Online (Sandbox Code Playgroud)

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

sol*_*oke 3

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

{% widthratio articlestat.score 1 TRENDING_PCT_FLOOR as ratio %}
{% if ratio >= articlestat.weighted_score %}style="font-weight:bold;"{% endif %}
Run Code Online (Sandbox Code Playgroud)