当前位置:网站首页>Day04 routing layer
Day04 routing layer
2022-07-19 01:22:00 【Wbig】
List of articles
1. Static routing
1.1 Static route matching
stay urls.py In the file urlpatterns Configure routing .
urlpatterns Properties of
As long as the path is the same , Whether it's GET,POST, All fall into one category of requests
path and re_path
path('index/',views.IndexView.as_view()), # Normal routing
re_path(r'^[a-z]{5}/$',views.IndexView.as_view()), # Regular expressions , As long as the conditions are met, you can get the page
Static routing
from django.http import HttpResponse
def MyView(request):
return HttpResponse(' View !')
from django.urls import path,re_path
from myapp import views
urlpatterns = [
path('',views.MyView),
re_path(r"^",views.MyView),
]
2. Dynamic routing
2.1 Dynamic route parameter transmission and dynamic route converter
# Create class view
class IndexView2(View):
def get(self,request,id):
return HttpResponse(f" Match to id by {id}")
# Distribution route
from django.urls import path,re_path
from myapp import views
urlpatterns = [
# Dynamic routing Specify the type
path('index2/<int:id>/',views.IndexView2.as_view()),
]
Regular grouping
# Create class view
class IndexView3(View):
def get(self,request,x,y):
return HttpResponse(f" Match to x by {x},y by {y}")
# Configure the routing
from django.urls import path,re_path
from myapp import views
urlpatterns = [
# Regular grouping
re_path(r'^(?P<x>\d+)/(?P<y>[a-z,A-Z]+)/$',views.IndexView3.as_view())
]
Two commonly used data types
str: string matching , By default
int: integer , The matching numbers also include 0
2.2 Dynamic routing participation GET The similarities and differences of Chuan Shen
Dynamic routing parameter transmission : Parameters participate in route matching , Get parameters in the route
GET The ginseng : Parameters do not participate in route matching , Get parameters in view
3. Routing distribution
3.1 The concept of routing distribution
There are too many routes that need to be configured for the main route , Just distribute the route to the sub application , Let sub applications manage their own routes
3.2 Implementation of routing distribution
Create a new one under the folder of the sub application urls.py Folder , Import the module of the main route .
Import in the main route include.
# Configuration of main route
from django.contrib import admin
from django.urls import path,include
from myapp import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include(urls)),
]
# Routing configuration in sub applications
from django.urls import path,re_path
from myapp import views
urlpatterns = [
path('register/',views.RegisterView.as_view()),
path('login/',views.LoginView.as_view()),
path('index/',views.IndexView.as_view()),
]
summary
After defining the class view in the view layer , Remember to configure the route !!!
边栏推荐
猜你喜欢

Pytoch implements linear regression manually

渗透测试信息收集总结
![[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)](/img/a6/410d81ad374e32e5cabd2eb242a183.png)
[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)

flutter项目中 advance_image_picker 组件使用

Oracle 数据库启动和关闭步骤

Build your own blog system website in one hour

深度之眼三——(7,8)】数学:矩阵对角化及二次型2.3

Lambda相关图形

How typora inserts tables

Record a bug that failed to pass the authentication of calling feign interface in the scheduled task
随机推荐
Calculation method of vector in torch
西瓜书+南瓜书第1-2章
Pytoch uses NN to realize softmax regression
数学03-导数与微分(待补)
kq-权限控制
Eye of depth III - (4, 5)] mathematics: matrix eigenvalues and eigenvectors 2
[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)
Oracle 数据库参数更改
深度之眼三——(6)】数学:矩阵对角化及二次型1
Oracle automatic storage management 18C step-by-step installation -1
Oracle database 12C parameter files (SPFILE and pfile)
Markdown各种数学符号
2021-3-19-字节-面值
CLI and vite realize cross domain through proxy
Oracle 数据库启用归档日志模式和归档日志删除和生成频率
记录一次easy_sql堆叠注入
mysql入门基础-dljd-老杜
1.互联网基础
Oracle automatic storage management 18C step-by-step installation -2
flutter 项目 ScrollController attached to multiple scroll views,Failed assertion: line 109 pos 12 报错处理