1.首先需要一个判断用户是否拥有这个权限的name来区分在主页上是否显示标签
在permission中加入
name=models.CharField(max_length=32, verbose_name=’url别名’,default=””)
2.在rabc.py 中加入一个空列表,用来存放关联的permission__name 来区分
左后注入session中
request.session["permission_names"] = permission_names3.在标签处写入判断
{% extends 'web/layout.html' %} {% block content %} <div class="luffy-container">
<div class="btn-group" style="margin: 5px 0">
{% load web %}
{% if "customer_add"|has_permission:request %}
<a class="btn btn-default" href="/customer/add/" rel="external nofollow" >
<i class="fa fa-plus-square" aria-hidden="true"></i> 添加客户
</a>
{% endif %} </div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>客户姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>公司</th> {% if "customer_edit"|has_permission:request%}
<th>编辑</th>
{% endif %}
{% if "customer_del"|has_permission:request %}
<th>删除</th>
{% endif %}
</tr>
</thead>
<tbody>
{% for row in data_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.name }}</td>
<td>{{ row.age }}</td>
<td>{{ row.email }}</td>
<td>{{ row.company }}</td> {% if "customer_edit"|has_permission:request %}
<td>
<a style="color: #333333;" href="/customer/edit/{{ row.id }}/" rel="external nofollow" >
<i class="fa fa-edit" aria-hidden="true"></i>
</a>
</td>
{% endif %} {% if "customer_del"|has_permission:request %}
<td>
<a style="color: #d9534f;" href="/customer/del/{{ row.id }}/" rel="external nofollow" >
<i class="fa fa-trash-o"></i>
</a>
</td>
{% endif %} </tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
customer_list
4.引用的web
from django.utils.safestring import mark_safe
from django.template import Library
import re
register =Library() @register.inclusion_tag("rbac/menu.html")
def get_menu_styles(request):
permission_menu_dict = request.session.get("permission_menu_dict")
print("permission_menu_dict",permission_menu_dict) for val in permission_menu_dict.values():
for item in val["children"]:
val["class"]="hide" ret=re.search("^{}$".format(item["url"]),request.path)
if ret:
val["class"] = "" return {"permission_menu_dict":permission_menu_dict} @register.filter
def has_permission(btn_url,request):
permission_names = request.session.get("permission_names") return btn_url in permission_names ''' {
1:{
"title":"信息管理",
"icon":"",
"children":[
{
"title":"客户列表",
"url":"",
}
] }, 2:{
"title":"财务管理",
"icon":"",
"children":[
{
"title":"缴费列表",
"url":"",
},
] }, } '''
web