当前位置:网站首页>第四单元 路由层
第四单元 路由层
2022-08-02 14:01:00 【czy1206527605】
静态路由及动态路由
静态路由
概念:
静态路由用来映射对应视图函数,其中urlpatterns是路由文件中的一个全局变量,用来存放路由及视图函数的映射关系
作用:
用于固定地址的路由,一般作业中使用的路由都是静态路由
动态路由
作用:
较为复杂,当路由设置在改变的时候,就要用到动态路由。
path动态路由
path('index2/<int:id>/',views.IndexView2.as_view()),
class IndexView2(View):
def get(self,request,id):
return HttpResponse(f"匹配到的id为{id}")
re.path动态路由
PS:使用正则表达式来编写
re_path(r'^(?P<x>\d+)/(?P<y>[a-zA-Z]+)/$',views.IndexView3.as_view())
class IndexView3(View):
def get(self,request,x,y):
return HttpResponse(f"x为{x},y为{y}")
访问连接:**http://127.0.0.1:8000/abc/123/**以获取到x和y的值
路由分发
概念:一般路由是直接写到主项目的url里的。而在主项目创建多个自定义应用的时候则需要运用到路由分发include将路由分给到各个应用中。
运用:
from myapp import urls
urlpatterns = [
path('',include(urls))
]
PS:
需要在各个应用内创建urls.py文件来将url地址存放到里面。
路由分发的优点
1.路由分发为我们带来的好处有很多,可以让我们在多个app的项目中更加方便有效的管理每一个路由
2.可以让我们的用户在访问时看到浏览器中的URL地址更加赏心悦目
边栏推荐
- rpm包的卸载与安装[通俗易懂]
- 文件加密软件有哪些?保障你的文件安全
- A number of embassies and consulates abroad have issued reminders about travel to China, personal and property safety
- 【ONE·Data || 排序入门】
- Go语言初始
- drf序列化器-Serializer
- Flutter 实现光影变换的立体旋转效果
- Sentinel源码(一)SentinelResourceAspect
- 【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'
- RowBounds[通俗易懂]
猜你喜欢

Sentinel源码(六)ParamFlowSlot热点参数限流

Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush

此次519暴跌的几点感触 2021-05-21

海明校验码纠错设计原理

Diodes and their applications

【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'

目标检测场景SSD-Mobilenetv1-FPN

Image retrieval method based on deep learning!

理解TCP长连接(Keepalive)

打破文件锁限制,以存储力量助力企业增长新动力
随机推荐
网络安全第二次作业
Sentinel源码(四)(滑动窗口流量统计)
Break the limit of file locks and use storage power to help enterprises grow new momentum
Go语言初始
Swagger 的使用
网络安全第三次作业
stack && queue
Large and comprehensive pom file example
C# 编译错误:Compiler Error CS1044
WeChat Mini Program-Recent Dynamic Scrolling Implementation
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
Gstreamer Plugin注册流程详解
Detailed explanation of stored procedures
C# using 使用方法
binary search && tree
OpenMMLab简介
About the development forecast of the market outlook?2021-05-23
redis分布式锁和看门狗的实现