现在项目使用了laymini的后台框架,非常不错,美中不足,没有构建菜单的python示例,遂自己写了一个,供大家参考:
数据库表结构
CREATE table `system_menu` ( `ID` int(11) unsigned NOT NulL auto_INCREMENT COMMENT 'ID', `pID` int(11) unsigned NOT NulL DEFAulT '0' COMMENT '父ID', `Title` varchar(100) NOT NulL DEFAulT '' COMMENT '名称', `icon` varchar(100) NOT NulL DEFAulT '' COMMENT '菜单图标', `href` varchar(100) NOT NulL DEFAulT '' COMMENT '链接', `target` varchar(20) NOT NulL DEFAulT '_self' COMMENT '链接打开方式', `sort` int(11) DEFAulT '0' COMMENT '菜单排序', `status` tinyint(1) unsigned NOT NulL DEFAulT '1' COMMENT '状态(0:禁用,1:启用)', `remark` varchar(255) DEFAulT NulL COMMENT '备注信息', `create_at` timestamp NulL DEFAulT NulL COMMENT '创建时间', `update_at` timestamp NulL DEFAulT NulL COMMENT '更新时间', `delete_at` timestamp NulL DEFAulT NulL COMMENT '删除时间', PRIMARY KEY (`ID`), KEY `Title` (`Title`), KEY `href` (`href`)) ENGINE=InnoDB auto_INCREMENT=250 DEFAulT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='系统菜单表';
python 示例
from flask import Jsonify, request, Blueprint, Json, make_responsefrom flask_login import login_required, current_user,login_user,logout_user,utilsfrom app import commonfrom app import loginManagerfrom app.models.systemmenu import SystemMenudef init_API(app): @app.route('/xxx/API/v1.0/menus', methods=['GET']) def getMenu(): systemMenu = SystemMenu() menuList = systemMenu.getByStatus(1) if menuList: homeinfo={"Title":"首页","href":"page/welcome-1.HTML?t=1"} logoinfo={"Title":"LAYUI MINI","image":"images/logo.png","href": ""} menuList = buildMenuChild(0, menuList)
# 合成菜单 menuAllList = dict() menuAllList["homeInfo"] = homeinfo menuAllList["logoInfo"] = logoinfo menuAllList["menuInfo"] = menuList result = make_response(Jsonify(menuAllList)) else: result = None return result # 递归创建菜单列表 def buildMenuChild(pID,menuList): treeList = dict() node = [] for item in menuList: if (item.pID == pID): nodetmp = {"Title":item.Title,"icon":item.icon,"href":item.href,"target":item.target} child = buildMenuChild(item.ID,menuList) if (child != {}): nodetmp.update({"child":child}) node.append(nodetmp) treeList = node return treeList
总结
以上是内存溢出为你收集整理的layuimini父子菜单-python实现全部内容,希望文章能够帮你解决layuimini父子菜单-python实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)