我需要允许用户根据他的权限查看页面的某些部分。
我在这里看到了一些答案,例如使用@user_passes_test
(Django - user permissions to certain views?)并检查模板(Should I use Django permissions checks in the template AND the view?)。
在后者 @ e-satisf '回答中指出,对于我的情况,我应该在模板上使用支票。
我的问题是:我怎样才能这样做?这样做的最佳方式(性能和安全性)是什么?
为了举例说明我的需要,我想到了Facebook。在Facebook中,您允许某些用户查看您的个人资料页面的所有部分或全部内容。这正是我需要实施的行为。
感谢您的想法。
感谢。
答案 0 :(得分:0)
它在django docs
中基本上
{% if perms.foo %}
<p>You have permission to do something in the foo app.</p>
{% else %}
<p>You don't have permission to do anything in the foo app.</p>
{% endif %}
答案 1 :(得分:0)
根据您的示例,我假设您在Django包的上下文中表示权限,而不是身份验证检查(is_authenticated())。这实际上是非常简单的,并在答案 e-satisf '中给出here。
编辑看起来更好的答案。