在Django中使用ifequal [英] Using ifequal in Django

查看:181
本文介绍了在Django中使用ifequal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这里有一些愚蠢的东西,但是我试图使用ifequal来评估一个模板变量。

I'm sure there is something silly I'm missing here, but I'm trying to use ifequal to evaluate a template variable.

这是我的模型:

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)

...我在模板中使用这个:

...and I'm using this in the template:

{% ifequal user.userprofile_set.get student %}
You're a student! 
{% endifequal %}

当我打印出{{user.userprofile_set.get }}我得到:

When I simply print out {{ user.userprofile_set.get }} I get:

student

不知道我失踪了 - 任何帮助都不胜感激!

Not sure what I'm missing - any help is appreciated!

推荐答案

ifequal已被弃用...但我认为这样做:

ifequal is deprecated... but I think that this works:

{% ifequal user.userprofile_set.get.type "student" %}
    Your a student! 
{% endifequal %}

这篇关于在Django中使用ifequal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆