简体   繁体   English

如何在页面上检查用户的权限?

[英]How to check user's permissions on page?

I need to allow the user to see some parts of a page according to his permissions. 我需要允许用户根据其权限查看页面的某些部分。

I've seen some answers here like using @user_passes_test ( Django - user permissions to certain views? ) and checks on the template ( Should I use Django permissions checks in the template AND the view? ). 我在这里看到了一些答案,例如使用@user_passes_test@user_passes_test 某些视图的用户权限? )并检查模板( 我应该在模板和视图中使用Django权限检查吗? )。

In the latter @e-satis ' answer states that for my case I should use checks at the template. 在后一种情况下, @ e-satis的回答指出,就我而言,我应该在模板上使用检查。

My question is: How can I do this and what is the best way (performance and security wise) to do so? 我的问题是:我该怎么做?这样做的最佳方法是什么(在性能和安全性方面)?

To exemplify my need I thought about Facebook. 为了说明我的需求,我想到了Facebook。 In Facebook you allow certain users to see some parts of your profile page or all of it. 在Facebook中,您允许某些用户查看您的个人资料页面的某些部分或全部。 That's exactly the behavior I need to implement. 这正是我需要实现的行为。

I appreciate your thoughts. 感谢您的想法。

Thanks. 谢谢。

it's in the django docs 在Django 文档中

basically 基本上

{% 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 %}

Based on your examples, I'm assuming you mean permissions in the context of the Django package and not authentication-checking (is_authenticated()). 根据您的示例,我假设您的意思是在Django程序包上下文中的权限,而不是身份验证检查(is_authenticated())。 This is actually pretty straight-forward and given fully in an answer e-satis ' gives here . 这实际上是非常直接的和完全的答案电子SATIS给出'给出这里

edit Looks like a better answer was given. 编辑似乎给出了一个更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM