【发布时间】:2014-02-02 21:07:05
【问题描述】:
我有点卡住了。
情况是这样的。我有 2 个 for 循环。一个循环遍历类别,另一个循环遍历匹配集。问题来了:
我需要从该类别中获取所有匹配项...
这是我所拥有的:
{% for category in tournament.get_categories %}
<div class="line" style="margin-top: 25px; margin-bottom: 40px;"></div>
<div class="container">
<h3 class="margin-reset" id="{{ category.slug }}">{% trans "Matches schedule" %}<span> | {{ category.name }}</span></h3>
<table class="standard-table table">
<thead>
<tr>
<th>Match</th>
<th>Heure</th>
<th>Plateau</th>
<th>Équipe bleu</th>
<th>Équipe gris</th>
<th>Équipe noir</th>
<th>Résultat</th>
</tr>
</thead>
<tbody>
{% for match in tournament.match_set.filter(category=category) %}
<tr>
<td>{{ match.match_num }}</td>
<td>{{ match.time }}</td>
<td>{{ match.plateau }}</td>
<td><a href="{{ match.blue_team.get_absolute_url }}">{{ match.blue_team.name }}</a></td>
<td><a href="{{ match.grey_team.get_absolute_url }}">{{ match.grey_team.name }}</a></td>
<td><a href="{{ match.black_team.get_absolute_url }}">{{ match.black_team.name }}</a></td>
<td><a href="{{ match.get_absolute_url }}">{{ match.get_url_string }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
你们可能已经猜到了,我得到了这个错误: 无法解析来自“tournament.match_set.filter(category=category)”的剩余部分:“(category=category)”
我能做什么?
谢谢, 阿拉
编辑:
解决方法如下: 自定义标签:
@register.filter
def get_matches_by_category(value, category):
return value.match_set.filter(category=category)
及用途:
{{ tournament|get_matches_by_category:category }}
【问题讨论】: