Django变量不符合预期

Django变量不符合预期

我有以下代码:

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


当我将团队硬编码为“斯旺西”时,它将起作用并产生以下结果:

Swansea | Swansea
Arsenal | Swansea
Bournemouth | Swansea


但是,我真正想要的是:

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


但这不会产生任何结果,这是我所希望看到的惊喜:

Swansea | Swansea


因此x.straightredfixturelive.home_team似乎包含“斯旺西”,但不匹配。我什至尝试:

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


而且也没有产生任何结果。因此,即使认为它在网页上显示为“斯旺西”,它似乎也不匹配。也许是数据类型问题?

型号信息:

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)

最佳答案

您的问题是您正在将模型实例与字符串进行比较,因此它们永远都不相等。

根据您的模型,您可能想要以下内容:

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

关于python - Django变量不符合预期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41967866/

10-09 05:14