【发布时间】:2016-04-06 22:16:36
【问题描述】:
如何在模板中检查用户是否属于某个组?
在生成template 的view 中是可能的,但是如果我想在扩展模板的base.html 中检查它怎么办(它没有自己的视图功能)?
我所有的模板都扩展了base.html,因此在每个view 中检查它并不好。
base.html 包含上栏,其中应包含按钮,具体取决于group 登录用户所在的位置(客户、卖家)。
在我的base.html 中是:
{% if user.is_authenticated %}
这还不够,因为我必须对来自Customers 的用户和来自Sellers 的用户采取不同的行动。
所以我想要的是:
{% if user.in_group('Customers') %}
<p>Customer</p>
{% endif %}
{% if user.in_group('Sellers') %}
<p>Seller</p>
{% endif %}
【问题讨论】:
标签: python django django-admin django-permissions