Using regular expressions re_path

Provided re_path, introduced re_path 

 1 from django.urls import path,re_path 

Url is then disposed in the URlpatterns

 1 from django.contrib import admin
 2 from django.urls import path,re_path
 3 from . import views
 4 
 5 # 配置url
 6 
 7 urlpatterns = [
 8     path('admin/', admin.site.urls),
 9     path('index/', views.index),
10     path('detail/<int:pk>/', views.detail),  # int:路径转换器
11     #URl captured parameter rule: using '<variable name>' url parameters may be captured in the attempt to pass 
12      # conventional converters: STR: hits except '/' all strings path separator 
13 is      #             int: any integer 
14  
15  
16      #   path ( 'Student / <int: year> / <int: month the> /', views.student) \ 
. 17  # regex re_path 
18 is      # Python regular expressions named packets (<name> pattern? ) 
. 19      # Django path search portion searches only url parameter with a method and a request (get post) independent 
20 is      re_path (R & lt ' Student / (? P <year> \. 4 {D}) / (? P <month The> [0-9 ] |. 1 [0-2]) / ' , views.student)
 21 is ]

View corresponding code

. 1  from django.utils.httpwrappers Import the HttpResponse   # HTTP response 
2  
. 3  
. 4  DEF index (Request):
 . 5      return the HttpResponse ( ' . The Hello, the this IS My Django Project ' )
 . 6  
. 7  
. 8  DEF Detail (Request, PK):   # URL parameters captured 
. 9  
10      return the HttpResponse ( ' student number of good results for the% s ' % PK)
 . 11  
12 is  
13 is  DEF student (Request, year, month the):
 14      return the HttpResponse ( '% s% s monthly enrollment of students ' % (year, month The))

 

Guess you like

Origin www.cnblogs.com/X1-Zi/p/10962728.html