当前位置:网站首页>创建+注册 子应用_定义路由,全局路由与子路由
创建+注册 子应用_定义路由,全局路由与子路由
2022-07-03 01:38:00 【chuntian_tester】
目录
2.将子应用定义的视图添加到全局路由配置urls.py文件中
3.3.1 路由表中通过"<int:pk>"指定路由参数类型
一.创建注册子应用
1.创建子应用
命令行使用:
python manage.py startapp 子应用名
注意:区别创建django项目:django-admin startproject 项目名 .
如:python manage.py startapp projects

2.创建后注册子应用
全局配置文件settings.py中->INSTALLED_APPS=[ ]中注册子应用,如果不注册,此模块将不可以用
注册方式:
‘子应用名‘
或者
‘子应用名.apps.ProjectsConfig'

3.子应用结构
projects:
migrations 用于存放迁移脚本
__init__.py 为包文件
admin.py 用于配置admin后台管理站点
apps.py 用于配置子应用信息
models.py 用于定义模型类
tests.py 用于定义当前子应用的单元测试逻辑
views.py 用于定义子应用的业务逻辑实现
二.定义路由
1.子应用的views.py中定义视图

2.将子应用定义的视图添加到全局路由配置urls.py文件中

3.前端路由访问

三.全局路由与子路由
路由:前端访问路径与后端视图接口的一一映射关系就是路由。
3.1 全局路由与子路由
定义在项目中urls.py->urlpatterns 列表中的路由映射关系就是全局路由;
定义在子应用中的urls.py->urlpatterns列表中的路由映射关系就是子路由,
子路由配置后还要在全局路由表中使用include()函数注册才能生效。
3.2 子路由配置流程:
3.2.1 子应用views.py中注册视图接口

3.2.2 将视图接口配置到子应用的路由表中

3.2.3 将子应用路由表通过注册到全局路由注册表中
通过include(“子应用名.urls”)函数注册

3.2.4 前端访问

3.3 路由转换器
路由转换器支持类型有:int,str等
3.3.1 路由表中通过"<int:pk>"指定路由参数类型
视图接口中需要行参接收此pk

前端访问

3.3.2 正则匹配路由:re_path

边栏推荐
- Deep learning notes (constantly updating...)
- Network security - cracking system passwords
- Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
- Prohibited package name
- ByteDance data Lake integration practice based on Hudi
- Processing of tree structure data
- 网络安全-密码破解
- 深度学习笔记(持续更新中。。。)
- 小程序开发黑马购物商城中遇到的问题
- [error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
猜你喜欢

小程序开发的部分功能

What are MySQL locks and classifications

stm32F407-------ADC

PS remove watermark details

可视化yolov5格式数据集(labelme json文件)

树形结构数据的处理

深度(穿透)选择器 ::v-deep/deep/及 > > >

Trial setup and use of idea GoLand development tool
![[data mining] task 1: distance calculation](/img/72/a63cdfe32a7c438acf48a069d9bba1.png)
[data mining] task 1: distance calculation

Take you ten days to easily complete the go micro service series (II)
随机推荐
机器学习笔记(持续更新中。。。)
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
Network security - vulnerabilities and Trojans
Basic operation of view
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
leetcode961. Find the elements repeated N times in the array with length 2n
《上市风云》荐书——唯勇气最可贵
Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
Performance test | script template sorting, tool sorting and result analysis
网络安全-密码破解
Cloud native topic sorting (to be updated)
自定义组件、使用npm包、全局数据共享、分包
Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
网络安全-钓鱼
Leetcode(540)——有序数组中的单一元素
[AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
[camera topic] complete analysis of camera dtsi
网络安全-NAT网络地址转换
Network security - scanning and password explosion 2
Network security - cracking system passwords