django - 在 Django 中使用 ifequal

标签 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)

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

{% 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/

相关文章:

django - 什么/在哪里修改 django-registration 以与移动浏览器一起使用

django - 使用包含 defer 的查询集在 Django 中序列化

python - 使用django上传文件

javascript - 使用 django html 模板创建具有动态行和列的表

要包含的 Django 传递对象

python - models.py 越来越大,最好的方法是什么?

javascript - 将变量传递给被插入现有数组的字符串

Django RESTful API - django-piston 与 django-tastypie

javascript - Django:AJAX请求后加载模板

python - django 在同一模板中为不同的 for 循环重用 html block