Django - user permissions to certain views?

后端 未结 5 908
夕颜
夕颜 2020-12-14 01:45

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 t

5条回答
  •  独厮守ぢ
    2020-12-14 02:36

    You need to manage that manually, but it's pretty easy. Presumably there's an attribute that determines whether or not a group has permission to see a view: then you just decorate that view with either the permission_required decorator, if it's a simple question of whether the user has a particular Permission, or user_passes_test if it's a bit more complicated:

    @user_passes_test(lambda u: u.is_allowed_to_see_view_myview())
    def myview(request):
        ...etc...
    

    assuming that is_allowed_to_see_view_myview is some sort of method on the User object.

    The authentication docs are pretty comprehensive.

提交回复
热议问题