Using ifequal in Django

安稳与你 提交于 2019-12-08 00:48:04

问题


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

Here's my model:

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 %}

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

student

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


回答1:


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

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


来源:https://stackoverflow.com/questions/11733651/using-ifequal-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!