Django - 某些视图的用户权限? [英] Django - user permissions to certain views?
问题描述
从管理员看,您可以向用户或用户组分配权限:允许添加,更改或删除模型中的数据。
不错,但我还需要允许用户或用户组访问或不访问一组视图。我在我的网站上有一些类型的服务,所以我想允许一些用户访问某些服务(页面/视图),而不是其他。
那么我怎么能允许某些用户/用户组访问某些视图?谢谢!
无法添加或更改某个模型的用户将无法在管理员中看到。 p>
如果我们在谈论自定义创建的视图,那么您可以创建一些检查用户权限的内容,如果没有该权限,则返回404。权限与模型相关联,组可分配各种权限。
您可以向模型添加权限:
#myproject / myapp / models.py
class MyModel(models.Model):
class Meta:
permission =(
('permission_code','友好许可描述'),
)
然后你可以检查一个用户是否有这样的权限:
@user_passes_test(lambda u:u.has_perm(' myapp.permission_code'))
def some_view(request):
#...
使用权限,您可以使用管理界面轻松地添加或删除用户和组。
From the admin I see that you can allocate permissions to a user or a user group to :allow add, change or delete data from a model.
That is great, but I also need to allow a user or a user group to access or not a group of views. I have certain type of services on my web site so I want to allow some users to access a certain services (pages/views) but not others.
So how can I allow certain users/user groups access to certain views? Thank you!
Users that cannot add or change etc. a certain model, will not be able to see it in the admin.
If we are talking about your custom created views then you could create something which checks a user for a permission and returns a 404 if they do not have that permission. Permissions are linked to models and a group can be assigned various permissions.
You can add a permission to a model like this:
# myproject/myapp/models.py
class MyModel(models.Model):
class Meta:
permissions = (
('permission_code', 'Friendly permission description'),
)
Then you can check a if a user has permission like this:
@user_passes_test(lambda u: u.has_perm('myapp.permission_code'))
def some_view(request):
# ...
Using permissions you can then easily add or remove them from users and groups simply using the admin interface.
这篇关于Django - 某些视图的用户权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!