首页 > 解决方案 > Django可以在应用程序而不是模型上设置权限吗

问题描述

我想对我的项目进行权限控制。一些用户可以看到某些应用程序的条目,而其他用户则不能。我想在应用程序而不是模型上设置权限,我已经搜索过,但只找到了如何在模型上设置权限。所以,我想知道如何设置应用程序的权限。

标签: djangopython-2.7

解决方案


您可以制作装饰器以选择性地允许用户访问页面
制作此装饰器

def filter_users(func):

    def checker(request,*args,**kwargs):
         if some_condition: #This condition will tell you whether to allow this perticular user
             return func(request,*args,**kwargs)
         else:
             return render('invalid.html') #return a page telling the user that he is not allowed

    return checker

现在只需将此装饰器应用于您想要阻止“某些”用户访问的所有视图。

前任:

@filter_users
def some_view(request):
    #Do Something...

现在只有允许的用户才能看到视图,其余的都将得到无效页面

您可以将此装饰器应用于要限制访问的特定应用程序的所有视图


推荐阅读