1. django请求生命周期/在浏览器输入一个地址回车。
- 实现了wsgi协议(本质写一个socket服务端):
- wsgiref模块,适用开发测试。
- uwsgi模块,处理并发。
- 中间件
- 路由
- 视图
- orm
- 模板
2. 框架认识
- django:重武器,包含了很多方便的组件。
- flask:轻量级,丰富的第三方组件。
简单程序:flask可以完成。
大型程序:django合适。
3. django提供了哪些方便的组件?
- admin
- auth
- ORM
- form/modeform
- session
...
1. 二级菜单
2. 导航条
3. 粒度控制到按钮级别
1. 二级菜单
前夕:
- 一个url就是一个权限
- 人拥有的权限多少本质就是拥有多少一个url的访问权限。
a. 提供基础模板
把static和templates放在web下面
Django项目会先从项目下面最外层找,然后按照注册的app顺序在每个app目录下找
b. 二级菜单示例
在配置文件中写一个数据结构(字典或列表)
# ################################### 菜单结构 ############################################# MENU_LIST=[ { ‘title‘:‘用户管理‘, ‘icon‘:‘fa-clipboard‘, ‘children‘:[ {‘title‘:‘个人中心‘,‘url‘:‘/web/index/‘}, {‘title‘:‘用户列表‘,‘url‘:‘/web/user/‘}, ] }, { ‘title‘:‘商品管理‘, ‘icon‘:‘fa-clipboard‘, ‘children‘:[ {‘title‘:‘商品列表‘,‘url‘:‘/web/order/‘}, {‘title‘:‘活动列表‘,‘url‘:‘/web/center/‘}, ] } ]
from django.shortcuts import render from django.conf import settings # 推荐 即包含django内置的,也包含自己写入settings文件的 def index(request): return render(request,‘index.html‘,{‘menus‘:settings.MENU_LIST})
<div class="static-menu"> 这里写菜单 {% for item in menus %} <div class="item"> <div class="title"> <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }} </div> <div class="body"> {% for child in item.children %} <a href="{{ child.url }}">{{ child.title }}</a> {% endfor %} </div> </div> {% endfor %} </div>
c. 在模板中调用函数,需要将函数定义在某个特定的地方。
特定的地方:任意app目录下的templatetags目录中定义。
# simple_tag # 在此处定密函数(特殊要求) from django.template import Library # 导入mark_safe可以让数据安全的在页面显示 from django.utils.safestring import mark_safe from django.conf import settings # 必须叫register register = Library() @register.simple_tag def show_menu(a1): return mark_safe(‘<a>菜单1</a>‘) # inclusion_tag @register.inclusion_tag(‘menu.html‘) def get_menu(request): """ :param request: 请求相关的所有数据 :return: """ return {‘menus‘:settings.MENU_LIST}
<!--去读取配置文件中的MENU_LIST,自动在页面上显示数据--> <!---导入xxxxxxxx模块-> {% load xxxxxxxx %} <!--执行show_menu函数并传递参数--> {# {% show_menu ‘aaa‘%}#} <!--执行get_menu函数并传递参数 request相当于请求的数据,可以是其他参数--> {% get_menu request%}
d. 访问菜单默认选中
方式一:菜单默认选中时显示红色
from django.template import Library # 导入mark_safe可以让数据安全的在页面显示 from django.utils.safestring import mark_safe from django.conf import settings # 必须叫register register = Library() # inclusion_tag @register.inclusion_tag(‘menu.html‘) def get_menu(request): """ :param request: 请求相关的所有数据 :return: """ current_url = request.path_info return {‘menus‘:settings.MENU_LIST,‘current_url‘:current_url}
<div class="static-menu"> {# 这里写菜单#} {% for item in menus %} <div class="item"> <div class="title"> <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }} </div> <div class="body"> {% for child in item.children %} {% if current_url == child.url %} <a style="color: red" href="{{ child.url }}">{{ child.title }}</a> {% else %} <a href="{{ child.url }}">{{ child.title }}</a> {% endif %} {% endfor %} </div> </div> {% endfor %} </div>
方式二:将样式单独写在css文件中,在xxxxxxxx.py中进行判断
.static-menu a.active { color: #2F72AB; border-left: 2px solid #2F72AB; }
<div class="static-menu"> {# 这里写菜单#} {% for item in menus %} <div class="item"> <div class="title"> <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }} </div> <div class="body"> {% for child in item.children %} <a class="{{ child.class }}" href="{{ child.url }}">{{ child.title }}</a> {% endfor %} </div> </div> {% endfor %} </div>
import copy # simple_tag # 在此处定密函数(特殊要求) from django.template import Library # 导入mark_safe可以让数据安全的在页面显示 from django.utils.safestring import mark_safe from django.conf import settings # 必须叫register register = Library() # inclusion_tag @register.inclusion_tag(‘menu.html‘) def get_menu(request): """ :param request: 请求相关的所有数据 :return: """ new_menu_list = copy.deepcopy(settings.MENU_LIST) for item in new_menu_list: for child in item[‘children‘]: if request.path_info == child[‘url‘]: child[‘class‘] = ‘active‘ break return {‘menus‘:new_menu_list}
方式三:
# ################################### 菜单结构 ############################################# MENU_LIST = [ { ‘title‘: ‘用户管理‘, ‘icon‘: ‘fa-clipboard‘, ‘class‘: ‘hide‘, ‘children‘: [ {‘title‘: ‘个人中心‘, ‘url‘: ‘/web/index/‘}, {‘title‘: ‘用户列表‘, ‘url‘: ‘/web/user/‘}, ] }, { ‘title‘: ‘商品管理‘, ‘icon‘: ‘fa-clipboard‘, ‘class‘: ‘hide‘, ‘children‘: [ {‘title‘: ‘商品列表‘, ‘url‘: ‘/web/order/‘}, {‘title‘: ‘活动列表‘, ‘url‘: ‘/web/center/‘}, ] } ]
import copy # simple_tag # 在此处定密函数(特殊要求) from django.template import Library # 导入mark_safe可以让数据安全的在页面显示 from django.utils.safestring import mark_safe from django.conf import settings # 必须叫register register = Library() # inclusion_tag @register.inclusion_tag(‘menu.html‘) def get_menu(request): """ :param request: 请求相关的所有数据 :return: """ new_menu_list = copy.deepcopy(settings.MENU_LIST) flag = False for item in new_menu_list: for child in item[‘children‘]: if request.path_info == child[‘url‘]: child[‘class‘] = ‘active‘ item[‘class‘] = ‘‘ flag = True break if flag: break return {‘menus‘: new_menu_list}
<script> $(function () { $(‘.multi-menu .title‘).click(function () { $(this).next().toggleClass(‘hide‘); }); }) </script>
<div class="multi-menu"> {# 这里写菜单#} {% for item in menus %} <div class="item"> <div class="title"> <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }} </div> <div class="body {{ item.class }}"> {% for child in item.children %} <a class="{{ child.class }}" href="{{ child.url }}">{{ child.title }}</a> {% endfor %} </div> </div> {% endfor %} </div>
总结:写简单程序时,基于示例6进行实现。
原文:https://www.cnblogs.com/xingye-mdd/p/9840365.html