【问题标题】:Django Template - Need to use filter()Django 模板 - 需要使用 filter()
【发布时间】: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 }}

【问题讨论】:

    标签: django templates filter


    【解决方案1】:

    我创建了一个自定义模板标签并使用了该过滤器。

    这有点矫枉过正,但是……哦,好吧。

    【讨论】:

    • 我认为自定义模板标签是这种场景的正确解决方案(它绝对是最整洁的)。你能分享你的代码吗?我很想看看
    • 嗨,我不知道...我认为这有点矫枉过正,因为它只是一个简单的过滤器。我将代码添加到原始帖子中。
    • 谢谢。我不认为这是矫枉过正。有道理,干净整洁
    【解决方案2】:

    我在一个非常相似的案例中所做的是将类别列表传递给模板,为它们添加一个带有匹配项的新属性,例如:

    for category in categories:
        category.matches = Match.objects.filter(tournament=tournament, category=category)
    

    这有点慢,但你可以工作并减少查询次数

    之后我会将类别列表传递给模板

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 2023-03-10
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2017-09-28
      • 2021-03-11
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多