Ara Sivaneswaran
Ara Sivaneswaran

Reputation: 375

Django Template - Need to use filter()

I am kinda stuck.

Here is the situation. I have 2 for loops. One that loops through categories and the other one that loops through a match set. Here is the problem:

I need to get all the matches from that category...

Here is what I have:

{% 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 %}

As you guys probably guessed it, I get this error: Could not parse the remainder: '(category=category)' from 'tournament.match_set.filter(category=category)'

What can I do?

Thanks, Ara

EDIT:

Here is the solution: The custom tag:

@register.filter    
def get_matches_by_category(value, category):
    return value.match_set.filter(category=category)

And the use:

{{ tournament|get_matches_by_category:category }}

Upvotes: 0

Views: 59

Answers (2)

Alvaro
Alvaro

Reputation: 12037

What I have done in a very similar case is pass the list of categories to the template, adding them a new attribute with the matches, like:

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

It's kinda slow, but you can work it and reduce the number of queries

I would pass the categories list to the template after this

Upvotes: 1

Ara Sivaneswaran
Ara Sivaneswaran

Reputation: 375

I created a custom templatetag and used that filter.

It's kinda an overkill but...oh well.

Upvotes: 1

Related Questions