当前位置:网站首页>Unit 4 Routing Layer
Unit 4 Routing Layer
2022-08-02 14:18:00 【czy1206527605】
Static and dynamic routing
Static Routing
Concept:Static routing is used to map the corresponding view function, where urlpatterns is a global variable in the routing file, which is used to store the mapping relationship between routing and view functionseffect:Routes used for fixed addresses, and routes used in general operations are static routes
Dynamic Routing
Function:More complex, when the routing settings are changing, dynamic routing is used.
path dynamic routing
path('index2//', views.IndexView2.as_view()),class IndexView2(View):def get(self, request, id):return HttpResponse(f"The matched id is {id}")
re.path dynamic routing
PS: use regular expressions to write
re_path(r'^(?P\d+)/(?P[a-zA-Z]+)/$',views.IndexView3.as_view())class IndexView3(View):def get(self, request, x, y):return HttpResponse(f"x is {x}, y is {y}")
Access the connection: **http://127.0.0.1:8000/abc/123/** to get the x and y values
Route Distribution
Concept: The general route is written directly to the url of the main project.When creating multiple custom applications in the main project, you need to use route distribution include to distribute routes to each application.
Using:
from myapp import urlsurlpatterns = [path('', include(urls))]
PS:
You need to create a urls.py file in each application to store the url address in it.
Advantages of routing distribution
1. There are many benefits that route distribution brings us, which allows us to manage each route more conveniently and effectively in multiple app projects
2. It allows our users to see the browsing when visitingThe URL address in the server is more pleasing to the eye
边栏推荐
猜你喜欢
随机推荐
idea社区版下载安装教程_安装天然气管道的流程
[ROS](06)ROS通信 —— 话题(Topic)通信
【ROS】编译软件包packages遇到进度缓慢或卡死,使用swap
泡利不相容原理适用的空间范围(系统)是多大?
redis分布式锁和看门狗的实现
关于C#使用DateTime数据的细节
跑yolov5又出啥问题了(1)p,r,map全部为0
logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
瑞吉外卖笔记——第08讲读写分离
chapter6可视化(不想看版)
WeChat Mini Program-Recent Dynamic Scrolling Implementation
logback源码阅读(一)获取ILoggerFactory、Logger
第十四单元 视图集及路由
RHCE第一天作业
苏州大学:从 PostgreSQL 到 TDengine
关于密码加密的一点思路
【Tensorflow】AttributeError: module 'keras.backend' has no attribute 'tf'
微信小程序-最近动态滚动实现
海明校验码纠错设计原理
网络安全第二次作业