在 Django 中使用 ifequal

Jef*_*ell 3 django django-templates

我确信我在这里遗漏了一些愚蠢的东西,但我正在尝试使用 ifequal 来评估模板变量。

这是我的模型:

USER_TYPES = (
('instructor', 'Instructor'),
('student', 'Student'),
)


class UserProfile(models.Model):
    type = models.CharField(
        choices=USER_TYPES, max_length=12
    )
    user = models.ForeignKey(
        User, 
        unique=True
    )

    def __unicode__(self):
        return u'%s' % (self.type)
Run Code Online (Sandbox Code Playgroud)

...我在模板中使用它:

{% ifequal user.userprofile_set.get student %}
You're a student! 
{% endifequal %}
Run Code Online (Sandbox Code Playgroud)

当我简单地打印出 {{ user.userprofile_set.get }} 时,我得到:

student
Run Code Online (Sandbox Code Playgroud)

不知道我错过了什么 - 感谢任何帮助!

Goi*_*oin 6

ifequal 已被弃用...但我认为这是有效的:

{% ifequal user.userprofile_set.get.type "student" %}
    You're a student! 
{% endifequal %}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的评论 - 我最终使用了 `{% if user.userprofile_set.get.type == "student" %}` 并且效果很好!谢谢! (2认同)