adminLTE的自动化菜单

一.
  model
  
 1 from django.db import models
 2 
 3 
 4 # Create your models here.
 5 class MenuInfo(models.Model):
 6     parent = models.ForeignKey('MenuInfo', blank=True, null=True)
 7     name = models.CharField('菜单名称', max_length=15)
 8     menu_icon = models.ForeignKey('IconInfo', verbose_name='图标')
 9     url = models.CharField('网址', max_length=100, blank=True, null=True)
10     show = models.BooleanField('是否显示', default=False)
11     priority = models.IntegerField(verbose_name=u'显示优先级', null=True, blank=True, default=-1)
12 
13     class Meta:
14         db_table = 'MenuInfo'
15 
16 
17 class IconInfo(models.Model):
18     DisplayName = models.CharField(max_length=100)
19     ClassName = models.CharField(max_length=100)
20     SourceType = models.CharField(max_length=100)
21     CreateTime = models.DateTimeField(auto_created=True)
22 
23     class Meta:
24         db_table = 'IconInfo'
model的代码

二.

  模板中间件

  1.setting的设置

 1 TEMPLATES = [
 2     {
 3         'BACKEND': 'django.template.backends.django.DjangoTemplates',
 4         'DIRS': [os.path.join(BASE_DIR, 'templates')]
 5         ,
 6         'APP_DIRS': True,
 7         'OPTIONS': {
 8             'context_processors': [
 9                 'django.template.context_processors.debug',
10                 'django.template.context_processors.request',
11                 'django.contrib.auth.context_processors.auth',
12                 'django.contrib.messages.context_processors.messages',
13                 'adminLTE.context_processors.make_menus_processor',
14             ],
15             'builtins': [                                     # Add this section
16                 "debugtools.templatetags.debugtools_tags",   # Add this line
17             ],
18         },
19     },
20 ]
setting的设置

  2.中间件的代码
  2.1设置了一个全局变量,这样方便设置菜单的active的选中状态

 1 from django.utils.html import format_html
 2 from menuInfo.models import MenuInfo
 3 
 4 menu_active = ''
 5 
 6 def make_menus_html(menus, parent_id=None, current_parent_id=None, active=None):
 7     """
 8     menus = Menus.objects.all()
 9     :param menus: 寻找的对象,传一个queryset对象
10     :param parent_id: 父级菜单ID
11     :param current_parent_id: 当前父级菜单ID
12     :param active: 激活的菜单名
13     :return:
14     """
15     make_html = ""
16     for menu in menus:
17         child_menu_flag = "treeview"
18         menu_right_flag = '<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>'
19         child_menu = '<li class="{active}"><a href="{menu_url}"><i class="fa fa-circle-o"></i> {menu_name}</a></li>'
20         child_menu_html = '<ul class="treeview-menu">{make_child_menu_html}</ul>'
21         master_menu_html = """
22         <li class="{child_menu_flag} {active}">
23             <a href="{menu_url}"><i class="fa {menu_icon}"></i> <span>{menu_name}</span>{menu_right_flag}</a>
24             <ul class="treeview-menu">
25             {children_menu_html}
26             </ul>
27         </li>"""
28         children_menu_html = """
29         <li class="treeview">
30             <a href="{menu_url}"><i class="fa fa-circle-o"></i> <span>{menu_name}</span>{menu_right_flag}</a>
31             {child_menu_html}
32         </li>"""
33         parent = menu.parent  # 获取当前菜单的父级菜单
34         if current_parent_id == menu.id or (not parent and current_parent_id):
35             continue  # 如果当前父级菜单ID是自己或没有父级菜单且有当前父级ID则跳过本次循环
36         if not parent and current_parent_id is None:  # 如果没有父级菜单且当前父级ID是None
37             make_children_menu_html = make_menus_html(menus, parent_id=parent_id, current_parent_id=menu.id)
38             if not make_children_menu_html:
39                 menu_right_flag = ''
40             menu_icon = menu.menu_icon.ClassName
41             if hasattr(menu, 'icon_name'):
42                 menu_icon = menu.icon_name
43             active = menu_active.split('/')[1]
44             active = '/%s/' % active
45             if menu.url == active:
46                 active_menu = 'active'
47             else:
48                 active_menu = ''
49             make_master_menu_html = master_menu_html.format(child_menu_flag=child_menu_flag,
50                                                             active=active_menu,
51                                                             menu_url=menu.url,
52                                                             menu_icon=menu_icon,
53                                                             menu_name=menu.name,
54                                                             menu_right_flag=menu_right_flag,
55                                                             children_menu_html=make_children_menu_html)
56             make_html += make_master_menu_html
57         elif parent and current_parent_id == parent.id:  # 如果有父级且当前父级ID是自己的父级ID4
58             make_child_menu_html = make_menus_html(menus, parent_id=current_parent_id, current_parent_id=menu.id)
59             if make_child_menu_html:
60                 child_menu_html = child_menu_html.format(make_child_menu_html=make_child_menu_html)
61                 children_menu_html = children_menu_html.format(menu_url=menu.url,
62                                                                menu_name=menu.name,
63                                                                menu_right_flag=menu_right_flag,
64                                                                child_menu_html=child_menu_html)
65             else:
66                 if menu.url == menu_active:
67                     active_menu = 'active'
68                 else:
69                     active_menu = ''
70                 children_menu_html = child_menu.format(menu_url=menu.url, menu_name=menu.name, active=active_menu)
71             make_html += children_menu_html
72         else:
73             continue
74     return make_html
75 
76 
77 def make_menus_processor(request):
78     menus_obj = MenuInfo.objects.filter(show=0)
79     global menu_active
80     menu_active = request.path
81     menus = make_menus_html(menus=menus_obj, active=menu_active)
82     return {'menus': format_html(menus)}
模板中间件代码

 

 

转载于:https://www.cnblogs.com/fcmyoo/p/6251716.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值