2、url路由及模板渲染方式
- url基本概念和格式
- 概念
- 网址,全球统一资源定位符,作用:用来标识互联网上资源的地址。
- 格式
- 概念
- django的路由系统
- URLconf(URL配置)
- 路径报错信息查看
- path和re_path
- path
- 格式
- path(route(路径), views(视图),
kwargs=None(可省略), name=None(可省略))
- path(route(路径), views(视图),
- url中传递参数
- path(‘StudentSystem_values/<score>/’,views.StudentSystem_values)
- 访问:http://127.0.0.1:8000/StudentSystem_values/82/
- 使用路径转换器:<转换器:变量名>
- 第一种方式:“-”:path(‘StudentSystem_times/<int:year>-<int:month>/’,views.StudentSystem_times)
- 第二种方式:“/”:path(‘StudentSystem_times/<int:year>/<int:month>/’,views.StudentSystem_times)
- 第一种方式:“-”:path(‘StudentSystem_times/<int:year>-<int:month>/’,views.StudentSystem_times)
- 格式
- re_path
- 时间
- re_path(r’StudentSystem_times/(?P<year>\d{4})-(?P<month>\d[1-9]|1[0-2])/’,views.StudentSystem_times)
- re_path(r’StudentSystem_times/(?P<year>\d{4})-(?P<month>\d[1-9]|1[0-2])/’,views.StudentSystem_times)
- 分数
- re_path(r’StudentSystem_values/(?P<score>\d[0-9]|[0-9]|100)/’,
views.StudentSystem_values)
- re_path(r’StudentSystem_values/(?P<score>\d[0-9]|[0-9]|100)/’,
- 时间
- include:实现访问app
- app下views.py代码
- app下新建urls.py代码
- 项目根目录urls.py配置app下的视图
- 注意
- app下views.py代码
- kwargs
- name
- app_name
- 路由系统原理
- 1.根URLconf,从上往下找,第一个匹配到的路径,映射到对应的视图函数
- 2.根URLconf,include到app的URLconf,从上往下找,第一个匹配到的路径,映射到对应的视图函数
- 3.都找不到,返回页面404
- path
- django模板路径配置
- 通用情况下,在项目根目录下创建templates模板文件
- 项目根目录settings.py下配置templates模板路径
- templates下创建每个app名,存放html文件
- 通用情况下,在项目根目录下创建templates模板文件
- django模板渲染方式
- 新建html文件
- 渲染
- 访问成功
- 新建html文件