Alan Tingey
Alan Tingey

Reputation: 971

Django Variable Not Matching As Expected

I have the following code:

{% for x in fixtures %}
    {% if currentSelectedTeam1Name == "Swansea" %}
        <tr>
            <td colspan="6">
                {{x.straightredfixturelive.home_team}} | {{currentSelectedTeam1Name}}
            </td>
        </tr>
    {% endif %}
{% endfor %}

When I hardcode the team as "Swansea" it works and produces the following results:

Swansea | Swansea
Arsenal | Swansea
Bournemouth | Swansea

However, what I really want it to be is:

{% if currentSelectedTeam1Name == x.straightredfixturelive.home_team %}

But this produces no results which is a suprise as I would expect to see:

Swansea | Swansea

So x.straightredfixturelive.home_team seems to contain "Swansea" but does not match up. I even tried:

{% if x.straightredfixturelive.home_team == "Swansea" %}

And that produced no results as well. So even thought it displays on the webpage as "Swansea" it does not seem to match up. Maybe a data type issue?

Model Info:

class StraightredFixtureLive(models.Model):
    fixtureid = models.OneToOneField(
        StraightredFixture,
        on_delete=models.CASCADE,
        primary_key=True,
    )
    home_team = models.ForeignKey('straightred.StraightredTeam', db_column='hometeamid', related_name='home_fixtures_live')
    away_team = models.ForeignKey('straightred.StraightredTeam', db_column='awayteamid', related_name='away_fixtures_live')
    fixturedate = models.DateTimeField(null=True)
    fixturestatus = models.CharField(max_length=24,null=True)
    fixturematchday = models.ForeignKey('straightred.StraightredFixtureMatchday', db_column='fixturematchday')
    spectators = models.IntegerField(null=True)
    hometeamscore = models.IntegerField(null=True)
    awayteamscore = models.IntegerField(null=True)
    homegoaldetails = models.TextField(null=True)
    awaygoaldetails = models.TextField(null=True)
    hometeamyellowcarddetails = models.TextField(null=True)
    awayteamyellowcarddetails = models.TextField(null=True)
    hometeamredcarddetails = models.TextField(null=True)
    awayteamredcarddetails = models.TextField(null=True)

Upvotes: 2

Views: 27

Answers (1)

Alasdair
Alasdair

Reputation: 309099

Your problem is that you are comparing a model instance to a string, so they are never equal.

Depending on your models, you probably want something like:

{% if currentSelectedTeam1Name == x.straightredfixturelive.home_team.name %}

Upvotes: 2

Related Questions