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 tagshttps://docs.djangoproject.com/es/1.9/howto/custom-template-tags/? 权限管理django 自带有基本的权限管理 ,但粒度和限制权限的维度都只是针对具体的表,如果我们想根据业务功能来限制权限,那就得自己写了, 不过也不用完全自己的写,我们可以在django 自带的权限基础上轻松的实现扩展。? 自己写权限要注意:
想对一个功能实现权限控制,要做到只能过在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 = % ( |