二级菜单

1.修改表结构

 1 class Menu(models.Model):
 2         title = models.CharField(max_length=32, verbose_name='菜单')
 3         icon = models.CharField(max_length=32, verbose_name='图标', null=True, blank=True)
 4 
 5     class Permission(models.Model):
 6         """
 7         权限表
 8         """
 9         title = models.CharField(max_length=32, verbose_name='标题')
10         url = models.CharField(max_length=32, verbose_name='权限')
11         menu=models.ForeignKey("Menu",on_delete=models.CASCADE,null=True)

2.重新将二级菜单信息主人session中

from rbac.models import Role


def initial_sesson(user,request):
    """
    功能:将当前登录人的所有权限录入session中
    :param user: 当前登录人
    """
    # 查询当前登录人的所有权限列表
    # 查看当前登录人的所有角色
    # ret=Role.objects.filter(user=user)
    permissions = Role.objects.filter(user=user).values("permissions__url",
                                                        "permissions__title",
                                                        "permissions__menu__pk",
                                                        "permissions__menu__title",
                                                        "permissions__menu__icon"
                                                        ).distinct()
    print(permissions)

    permission_list = []
    permission_menu_dict = {}

    for item in permissions:
        # 构建权限列表
        permission_list.append(item["permissions__url"])

        # 构建菜单权限列表
        if item["permissions__menu__pk"]:
            if not item["permissions__menu__pk"] in permission_menu_dict:
                permission_menu_dict = {
                    "title":item["permissions__menu__title"],
                    "icon":item["permissions__menu__icon"],
                    "children":[
                        {
                            "title":item["permissions__title"],
                            "url":item["permissions__url"]
                        }
                    ]
                }
        else:
            permission_menu_dict["children"].append({
                "title":item["permissions__title"],
                "url":item["permissions__url"]
            })


    # 将当前登录人的权限列表注入session中
    request.session["permission_list"] = permission_list
    # 将当前登录人的菜单权限列表注入session中
    request.session["permission_menu_dict"] = permission_menu_dict

3.获取二级菜单信息

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)
    return {"permission_menu_dict":permission_menu_dict}

4.渲染界面

<div class="multi-menu">

    {% for item in permission_menu_dict.values %}

       <div class="item">
        <div class="title"><i class="{{ item.menu_icon }}"></i>{{ item.menu_title }}</div>
        <div class="body">
              {% for foo in item.children %}
                  <a href="{{ foo.url }}">{{ foo.title }}</a>
              {% endfor %}

        </div>
       </div>
    {% endfor %}

</div>

 

posted @ 2018-11-15 21:45  被嫌弃的胖子  阅读(203)  评论(0编辑  收藏  举报