Python flask多維字典實現選單動態新增
阿新 • • 發佈:2019-01-25
@staticmethod def insert_menus(): menus = { '系統管理': { '使用者管理': ['/user_manage', ''], '角色管理': ['', ''], '選單管理': ['', ''], '許可權管理': ['', ''], '日誌管理': ['', ''] }, '配置管理': { '專業管理': ['', ''], '科目管理': ['', ''] }, } for class1 in menus: class2s = menus[class1] for class2 in class2s: menu = Cuc_Menu.query.filter_by(menu_class1=class1, menu_class2=class2).first() if menu is None: menu = Cuc_Menu(menu_class1=class1, menu_class2=class2) menu.menu_url, menu.menu_memo = menus[class1][class2] db.session.add(menu) db.session.commit()
def get_menus(self): auths = Cuc_Auth.query.filter_by(role_id=self.role.id).all() menuMap = {} for auth in auths: menu = Cuc_Menu.query.filter_by(id=auth.menu_id).first() class1 = menu.menu_class1 class2 = menu.menu_class2 url = menu.menu_url memo = menu.menu_memo if class1 not in menuMap: menuMap[class1] = {} menuMap[class1][class2] = [url, memo] return menuMap
{% if current_user.is_authenticated %} <div class="col-sm-2"> <div class="div1" id="left_menu"> {% for m_item in menu_items %} <div class="div2"> <div class="jbsz"> </div> {{ m_item }} </div> <div class="div3"> <ul> {% for m_i in menu_items[m_item] %} <li><a href=".{{ menu_items[m_item][m_i][0] }}">{{ m_i }}</a></li> {% endfor %} </ul> </div> {% endfor %} </div> </div> {% endif %}
<script type="text/javascript"> $(document).ready(function () { $(".div2").click(function () { $(this).next("div").slideToggle("slow") .siblings(".div3:visible").slideUp("slow"); }); }); </script>