0
在每个页面(base.html
)中,我想检查request.user
是否具有来自我的类UserTypes
的管理员角色并显示管理员链接。目前,我做这样的事情:有没有更简单的方法来检查Django模板中的M2M值?
{% if user.profile.user_types.all %}
{% for user_type in user.profile.user_types.all %}
{% if user_type.name == "ad" %}
<li>
<a href="{% url admin:index %}" class="round button dark ic-settings image-left">Admin</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
user.profile
只是从Django的User
去我UserProfile
。
但是,这似乎有点冗长和笨重。有一种更简单的方法吗?也许我应该写我自己的自定义背景处理器和传递变量像is_admin
或东西,但我从来没有写过之前自定义背景处理器...
也许一个愚蠢的问题,但为什么不只是依靠'User.is_superuser'?或者,如果“管理员”是指任何有权访问Django管理员User.is_staff的用户。看起来你只是增加了额外的复杂性,没有很好的理由。 –