django crm 左側二級菜單欄

1.如何生成二級菜單欄html

  在 rbca表中生成一張表 menu , 表示可能須要給客戶展現的一級菜單欄django

  在permission表中 ,建立字段,經過多對一外鍵關係,關聯到menu表,表示哪些permission要在二級菜單欄顯示,顯示到哪一個一級菜單欄下面session

  在initial_session 權限注入的時候,注入一個字典 permission_menu_dict  > 格式以下:數據結構

'''目標數據結構
 { 1:{  # 1--menu_id 'title':'..', 'icon':'xx', children:[ {'title':'二級','url':'xx'}, {'title':'二級','url':'xx'}, ] }, 2:{  # 2--menu_id 'title':'..', 'icon':'xx', children:[ {'title':'二級','url':'xx'}, {'title':'二級','url':'xx'}, ] } } '''
rbac.py

  在自定義標籤中循環該字典生成標籤app

from django import template from django.urls import reverse register = template.Library() from django.conf import settings import re @register.inclusion_tag('rbac/menu.html') def menu(request): # menu_list = request.session.get('permisson_menu_list') menu_dict= request.session.get('permisson_menu_dict') for key,item in menu_dict.items(): item['class'] = 'hide'

        for child in item['children']: # /customer/add/ # 'url': '/customer/list/' # 'url': '/payment/list/' # # if re.match('^{}$'.format(child['url']), request.path): if request.show_id == child['pk']: # null == child['pk'] 1 item['class'] = '' child['class'] = 'active'
                break


    return {"menu_dict": menu_dict} @register.filter def haspermission(base_url,request): for item in request.session['permisson_list']: #item = {'url','','title':'','name':''} reg = '^%s$' % item['url']  #/customer/edit/(\d+)/ --- "/customer/edit/
        # /"         ret = re.search(reg, base_url) # if item['name'] == base_url: if ret: return True return False
rbac.py
from django.db import models class Menu(models.Model): title = models.CharField(max_length=32, verbose_name='一級菜單', null=True, blank=True) icon = models.CharField(max_length=32, verbose_name='一級菜單圖標', null=True, blank=True) class Permission(models.Model): """ 權限表 """ title = models.CharField(max_length=32, verbose_name='標題') url = models.CharField(max_length=32, verbose_name='權限') # is_menu = models.BooleanField(default=False, verbose_name='是不是菜單')
    menu = models.ForeignKey('Menu',null=True) pid = models.ForeignKey('self',null=True) class Meta: verbose_name_plural = '權限表' verbose_name = '權限表'
    
    def __str__(self): return self.title class Role(models.Model): name = models.CharField(max_length=32, verbose_name='角色名稱') permissions = models.ManyToManyField(to='Permission', verbose_name='角色所擁有的權限', blank=True) def __str__(self): return self.name class User(models.Model): """ 用戶表 """ name = models.CharField(max_length=32, verbose_name='用戶名') password = models.CharField(max_length=32, verbose_name='密碼') roles = models.ManyToManyField(to='Role', verbose_name='用戶所擁有的角色', blank=True) def __str__(self): return self.name
models.py
from rbac import models def initial_session(request,user_obj): permission_obj = models.Permission.objects.filter(role__user=user_obj).values('title','url','pk','menu_id','menu__title','menu__icon').distinct() # print(permission_obj)
 permission_list = [] permission_menu_dict = {} for item in permission_obj: permission_list.append(item['url']) if item['menu_id']: if item['menu_id'] not in permission_menu_dict: permission_menu_dict[item['menu_id']] = {'title':item['menu__title'],'icon':item['menu__icon'],'children':[{'title':item['title'],'url':item['url']}]} else: permission_menu_dict[item.menu_id]['children'].append({'title':item['title'],'url':item['url']}) # permission_list = [i.url for i in permission_obj]
    request.session['permission_list'] = permission_list request.session['permission_menu_dict'] = permission_menu_dict print(permission_menu_dict) ''' 1. 循環生成2級菜單 生成一個字典 permission_menu_dict = { 1:{} } '''
permission_input
<div class="static-menu"> {# {% for item in menu_list %}#}
{# {% if item.active %}#}
{# <a href="{{ item.url }}" class="{{ item.class }} active">#}
{# <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }}</a>#}
{# {% else %}#}
{# <a href="{{ item.url }}" class="{{ item.class }} ">#}
{# <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }}</a>#}
{# {% endif %}#}
{##}
{##}
{# {% endfor %}#}
        {% for item in menu_list.values %} <div class="item">
            <div class="title">
                <i class="{{ item.icon }}"></i>{{ item.title }} </div> {% for child in item.children %} <div class="body {{ item.class }}">
                    <a href="{{ child.url }}" class="{{ child.class }}">{{ child.title }}</a>
                </div> {% endfor %} </div> {% endfor %} </div>
menu.html
<script> $('.item .title').click(function () { $(this).next().toggleClass('hide'); $(this).parent().siblings().children(".body").addClass("hide") }); </script>
layout.html
相關文章
相關標籤/搜索