我只希望经理创建网站上的内容,因此我在模型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 %}
但没有任何改变。
我做错了什么?
答案 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 %}
由于已连接,您忘记了添加用户