我确定我在这里遗漏了一些愚蠢的东西,但我正在尝试使用 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)
...我在模板中使用了它:
{% ifequal user.userprofile_set.get student %}
You're a student!
{% endifequal %}
当我简单地打印出 {{ user.userprofile_set.get }} 我得到:
student
不确定我遗漏了什么 - 感谢您的帮助!
最佳答案
ifequal 已被弃用......但我认为这可行:
{% ifequal user.userprofile_set.get.type "student" %}
You're a student!
{% endifequal %}
关于django - 在 Django 中使用 ifequal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11733651/