减去模板中的两个字段 [英] Subtracting two fields in a template

查看:94
本文介绍了减去模板中的两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图循环访问模板中的数据库中的对象,我想显示的一个是表中两个字段的区别。有没有一个干净的方式来做到这一点:

I'm trying to loop through objects from my database in the template and one of the things I want to display is the difference between two of the fields in the table. Is there a clean way to do this:

{% for game in games %}
<tr>
    <td width=100><p>{{ game.name }} </p></td>
    <td width=300><p>{{ game.campaign }} </p></td>
    <td width=90><p>{{ game.num_Players }} </p></td>
    <td width=90><p>{{ game.num_Players - game.accepted_Characters }}</p></td>
    <td width=90><p>{{ game.standing_Requests }}</p></td>
</tr>
{% endfor %}

?哪里游戏是一个具有所有这些字段的表对象。

? Where game is a table object with all those fields.

推荐答案

Django模板被设计为不做计算。您应该在视图中调整游戏列表,然后将其显示在模板中:

Django templates are designed not to do computation. You should adjust your games list in the view, then display it in the template:

# views.py
for game in games:
    game.chars_diff = game.num_Players - game.accepted_Characters


# template.html
<td><p>{{ game.chars_diff }}</p></td>

这篇关于减去模板中的两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆