jAC
jAC

Reputation: 3435

Django - How to use a template tag in an if statement

I thought this idea would be more popular but I am guessing it's an easy solution that I cannot figure out. What I would like to do is in a Django template page, have an IF statement equal another tag value.

For example

views.py

def seasonstandings(request):
    divisions = Team.objects.order_by().values_list('division__name',flat=True).distinct()
    stats = WeeklyStats.objects.values('player__team__team_name').annotate(
        team=F('player__team__team_name'),
        points = Sum('finishes'),
        division = F('player__team__division__name')
    ).order_by('-points')

    return render(request, 'website/seasonstandings.html', {'divisions':divisions,'stats':stats})

seasonstandings.html

{% for division in divisions %}
{{ division }} <br>
  {% for stat in stats %}
    {% if stat.division = {{ division }} %}
    {{ stat.team }}<br>
    {% endif %}
  {% endfor %}
{% endfor %}

So if you note the IF statement is trying to use the result of the Division tag from the first for loop.

My main objective here is to have a dynamic list of divisions that a team could be in and then when they are assigned their division they would get listed out under the proper division based on these for loops.

The End Result looking like

Division A

Team 1

Team 2

Team 4

Division B

Team 3

Team 5

Division C

Team 6

Team 7

Any help is appreciated as always.

Upvotes: 0

Views: 612

Answers (1)

Pythonista
Pythonista

Reputation: 11615

Just drop the braces around division. Meaning {{ division }} -> division. and you can use ==.

The ifequal tag has been deprecated.

Upvotes: 3

Related Questions