使用django的权限管理系统permission
生活随笔
收集整理的這篇文章主要介紹了
使用django的权限管理系统permission
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.為model添加權限
class Task(models.Model):.......class Meta:permissions = (('oprater_task','can change the tasks'),)
2.views中可以使用如下方法來操作權限
3.在template中使用權限方法:
?4.permission提供的裝飾器
?5.user_passes_test的簡便用法
?
@user_passes_test(lambda u: u.is_superuser)#django源代碼中的user_passes_test def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):"""注意該函數的第一個參數Decorator for views that checks that the user passes the given test,redirecting to the log-in page if necessary. The test should be a callablethat takes the user object and returns True if the user passes."""def decorator(view_func):@wraps(view_func, assigned=available_attrs(view_func))def _wrapped_view(request, *args, **kwargs):if test_func(request.user):return view_func(request, *args, **kwargs)path = request.build_absolute_uri()# If the login url is the same scheme and net location then just# use the path as the "next" url.login_scheme, login_netloc = urlparse.urlparse(login_url orsettings.LOGIN_URL)[:2]current_scheme, current_netloc = urlparse.urlparse(path)[:2]if ((not login_scheme or login_scheme == current_scheme) and(not login_netloc or login_netloc == current_netloc)):path = request.get_full_path()from django.contrib.auth.views import redirect_to_loginreturn redirect_to_login(path, login_url, redirect_field_name)return _wrapped_viewreturn decorator?
6.判斷是否為超級管理員的過濾器:
@user_passes_test(lambda u: u.is_superuser)
?
轉載于:https://www.cnblogs.com/chenjianhong/archive/2013/03/08/4144818.html
總結
以上是生活随笔為你收集整理的使用django的权限管理系统permission的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Oracle 学习] Oracle中将
- 下一篇: java信息管理系统总结_java实现科