权限系统
页面信息的动态显示
def m1(request): #按照以上的公式,将菜单组成一个字典,字典的组成如下形式如下 组成好字典后将字典的值传给HTML页面渲染好 menu_dict = { 1: { "title": "菜单一", #active 字段表示是否被选中,如果是被选中的,则值为True 如何不是被选中的则值为False "active": False, "children": [ {"title": "添加用户", "url": "xxxxxxxxxxx", "active": False}, {"title": "查看用户", "url": "xxxxxxxxxxx", "active": False}, ]}, 2: { "title": "菜单二", #此处有被选中的,则active的值为True "active": True, "children": [ {"title": "添加用户", "url": "xxxxxxxxxxx", "active": False}, #被选中的菜单的值也是Ture {"title": "查看用户", "url": "xxxxxxxxxxx", "active": True}, ] }} return render(request,"m1.html",locals())
HTML页面代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <style> .header { width: 100%; height: 50px; background-color: #336699; } .menu, .content { float: left; } .menu { width: 200px; height: 600px; background-color: darkgray; } # 设置样式,没有被选中的 添加此样式隐藏菜单详细信息 .hide { display: none; } .menu .title { font-size: 16px; color: #336699 !important; margin: 20px 0; } .con a { margin-left: 30px; color: white; } #此样式用于显示被选中的额详细菜单的颜色加红 .active { color: red !important; } </style> </head> <body> <div class="header"></div> <div class="box"> <div class="menu"> #将字典传过来 取出字典的值 {% for item in menu_dict.values %} <div class="item"> <div class="title"><a href="">{{ item.title }}</a></div> #判断 active的值是否为Ture 当是True时,样式不加 hide {% if item.active %} <div class="con"> #否则 样式加hide {% else %} <div class="con hide"> {% endif %} #再在循环中打印 item的children中的值, {% for son in item.children %} #判断这个菜单是否是被选中的,是的话就加上红色的样式 {% if son.active %} <p><a href="{{ son.url }}" class="active">{{ son.title }}</a></p> #不是的话不加 {% else %} <p><a href="{{ son.url }}">{{ son.title }}</a></p> {% endif %} {% endfor %} </div> </div> {% endfor %} </div> <div class="content"> {% block con %} {% endblock %} </div> </div> </body> </html>
浙公网安备 33010602011771号