加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python之路,Day16 - Django 进阶

发布时间:2020-12-17 00:05:54 所属栏目:Python 来源:网络整理
导读:本节内容 自定义template tags 中间件 CRSF 权限管理 分页 Django分页 https://docs.djangoproject.com/en/1.9/topics/pagination/? 自定义template tags https://docs.djangoproject.com/es/1.9/howto/custom-template-tags/? 权限管理 django 自带有基本的

本节内容

自定义template tags

中间件

CRSF

权限管理

分页

Django分页

https://docs.djangoproject.com/en/1.9/topics/pagination/?

自定义template tags

https://docs.djangoproject.com/es/1.9/howto/custom-template-tags/?

权限管理

django 自带有基本的权限管理 ,但粒度和限制权限的维度都只是针对具体的表,如果我们想根据业务功能来限制权限,那就得自己写了, 不过也不用完全自己的写,我们可以在django 自带的权限基础上轻松的实现扩展。?

自己写权限要注意:

  1. 权限系统的设计对开发者、用户要实现透明,即他们不需要改变自己原有的使用系统或调用接口的方式
  2. 权限要易扩展,灵活
  3. 权限要能实现非常小的粒度的控制,甚至细致到一个按键某个用户是否能按。

想对一个功能实现权限控制,要做到只能过在views方法上加一个装饰器就行了,比如:

if request.method == 'POST': customer_form = forms.CustomerDetailForm(request.POST,instance=customer_obj) if customer_form.is_valid(): customer_form.save() parent_base_url = '/'.join(request.path.split('/')[:-2]) print("url:",parent_base_url ) return redirect(parent_base_url) else: print(customer_form.errors) return render(request,'crm/customer_detail.html',{'customer_form':customer_form})

check_permission的代码实现

= django.core.urlresolvers django.shortcuts perm_dic = : [, : [, : [,,[ perm_check(*args,** request = url_resovle_obj = current_url_namespace = ( matched_flag = False matched_perm_key = current_url_namespace None: ( perm_key perm_val = len(perm_val) == 3: url_namespace,request_method,request_args = url_namespace == current_url_namespace: request.method == request_method: request_args: matched_flag = matched_perm_key = ( request_arg request_args: request_method_func = getattr(request,request_method) >>",request_method_func.get(request_arg)) request_method_func.get(request_arg) matched_flag = True matched_flag = ( % matched_flag == True: ( matched_perm_key = : matched_flag == perm_str = % ( ( ( wrapper(*args,** ( perm_check(*args,** render(args[0], func(*args,** wrapper

  

Middleware中间件?

https://docs.djangoproject.com/es/1.9/topics/http/middleware/#process_request?

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读