django - Django可以在应用程序而不是模型上设置权限吗
问题描述
我想对我的项目进行权限控制。一些用户可以看到某些应用程序的条目,而其他用户则不能。我想在应用程序而不是模型上设置权限,我已经搜索过,但只找到了如何在模型上设置权限。所以,我想知道如何设置应用程序的权限。
解决方案
您可以制作装饰器以选择性地允许用户访问页面
制作此装饰器
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...
现在只有允许的用户才能看到视图,其余的都将得到无效页面
您可以将此装饰器应用于要限制访问的特定应用程序的所有视图
推荐阅读
- python - 尝试在 Python 中使用 Selenium 为用户名和密码选择一个字段
- java - 我如何在没有任何 gui 的 Linux 服务器上使用 java Save All Resources in chrome headless?
- c - 这个 GCC 警告是什么意思?
- here-api - 这里 Maps Routing Api 返回 404 5 次中有 1 次
- php - 重定向页面时如何将数据发布到控制器
- python - 如何在电子游戏中赋予 AI 控制权?
- javascript - 从 Firebase 更新数据时出现“坐标”错误
- logging - 如何审核 Key Vault 中的密钥访问
- c - C中的struct和typedef struct之间的真正区别是什么?
- python - 我应该为 flask.ext.login 和 flask.login 安装哪个包?