1.首先需要一个判断用户是否拥有这个权限的name来区分在主页上是否显示标签
在permission中加入
name=models.CharField(max_length=32, verbose_name='url别名',default="")
2.在rabc.py 中加入一个空列表,用来存放关联的permission__name 来区分
左后注入session中
request.session["permission_names"] = permission_names
3.
在标签处写入判断
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 {% extends 'web/layout.html' %} 2 3 {% block content %} 4 5 <div class="luffy-container"> 6 <div class="btn-group" style="margin: 5px 0"> 7 {% load web %} 8 {% if "customer_add"|has_permission:request %} 9 <a class="btn btn-default" href="/customer/add/"> 10 <i class="fa fa-plus-square" aria-hidden="true"></i> 添加客户 11 </a> 12 {% endif %} 13 14 </div> 15 <table class="table table-bordered table-hover"> 16 <thead> 17 <tr> 18 <th>ID</th> 19 <th>客户姓名</th> 20 <th>年龄</th> 21 <th>邮箱</th> 22 <th>公司</th> 23 24 {% if "customer_edit"|has_permission:request%} 25 <th>编辑</th> 26 {% endif %} 27 {% if "customer_del"|has_permission:request %} 28 <th>删除</th> 29 {% endif %} 30 </tr> 31 </thead> 32 <tbody> 33 {% for row in data_list %} 34 <tr> 35 <td>{{ row.id }}</td> 36 <td>{{ row.name }}</td> 37 <td>{{ row.age }}</td> 38 <td>{{ row.email }}</td> 39 <td>{{ row.company }}</td> 40 41 {% if "customer_edit"|has_permission:request %} 42 <td> 43 <a style="color: #333333;" href="/customer/edit/{{ row.id }}/"> 44 <i class="fa fa-edit" aria-hidden="true"></i> 45 </a> 46 </td> 47 {% endif %} 48 49 {% if "customer_del"|has_permission:request %} 50 <td> 51 <a style="color: #d9534f;" href="/customer/del/{{ row.id }}/"> 52 <i class="fa fa-trash-o"></i> 53 </a> 54 </td> 55 {% endif %} 56 57 58 </tr> 59 {% endfor %} 60 </tbody> 61 </table> 62 </div> 63 {% endblock %}
4.引用的web
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from django.utils.safestring import mark_safe 2 from django.template import Library 3 import re 4 register =Library() 5 6 7 @register.inclusion_tag("rbac/menu.html") 8 def get_menu_styles(request): 9 permission_menu_dict = request.session.get("permission_menu_dict") 10 print("permission_menu_dict",permission_menu_dict) 11 12 for val in permission_menu_dict.values(): 13 for item in val["children"]: 14 val["class"]="hide" 15 16 ret=re.search("^{}$".format(item["url"]),request.path) 17 if ret: 18 val["class"] = "" 19 20 return {"permission_menu_dict":permission_menu_dict} 21 22 23 24 25 26 27 @register.filter 28 def has_permission(btn_url,request): 29 permission_names = request.session.get("permission_names") 30 31 return btn_url in permission_names 32 33 34 35 36 37 38 39 40 41 ''' 42 43 { 44 1:{ 45 "title":"信息管理", 46 "icon":"", 47 "children":[ 48 { 49 "title":"客户列表", 50 "url":"", 51 } 52 ] 53 54 }, 55 56 2:{ 57 "title":"财务管理", 58 "icon":"", 59 "children":[ 60 { 61 "title":"缴费列表", 62 "url":"", 63 }, 64 ] 65 66 }, 67 68 69 } 70 71 72 73 74 '''