根据布尔显示不同的内容

时间:2019-11-26 20:00:27

标签: python django django-templates

我只希望经理创建网站上的内容,因此我在模型Profile中添加了一个条目

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    manager = models.BooleanField(default=False)

默认为假

如果为false,则用户看不到内容。

所以我尝试了:

{% if profile == manager %}
    SHOW THE CONTENT
{% else %}
    Does not show the content
{% endif %}

但没有任何改变。

我做错了什么?

3 个答案:

答案 0 :(得分:0)

您在模型上添加了一个属性。

您的Profile对象不会==经理。您需要访问Profile对象上的属性。

尝试

{% if profile.manager %}

这将检查配置文件对象的manager属性是否为True

答案 1 :(得分:0)

如果尚未覆盖视图context,则需要使用object访问字段。

{% if object.manager %}
    SHOW THE CONTENT
{%else%}
    Does not show the content
{% endif %}

答案 2 :(得分:0)

您需要这样做:

  {% if user.profile.manager %}
    SHOW THE CONTENT
{%else%}
    Does not show the content
{% endif %}

由于已连接,您忘记了添加用户