django2.x version url configuration

1, import module

from django.urls import path, include, re_path

 

Routing system process:

1) Enter the URL of the page, submit a request

2) found in settings.py Root_URLCONF, into the routing system in order to find the match in accordance

3) found, call the view function map matching url, and passes the request parameters as well as other objects request HttpRequest

4) view function processing, returns to the browser display HttpResponse (the browser parses the page)

5) no match is found on the error of url

 

2, generally static configuration article / 2017 /

path (route, views. function name, the function argument, the alias url mode)

= the urlpatterns [ 
path ( "ADMIN / ', admin.site.urls),
path (' Hello / ', views.hello), # preceding version is represented empty' ^ $ ', the new version directly' end / constant to, Hello not, necessarily Hello /
path ( 'Time /', views.current_time),
]


3, dynamic configuration    <Type: variable name >, articles / <int: year > /

path('article/<int:year>/',views.book)

Variable name passed as a parameter to the view function; types are int, str, slug, Uuid, path

 

4, the regular matching re_path () (? P <year> [0-9] {4})  

Use re_path () function; all expressed str format, not other types.

Two forms: not extracted parameters such re_path (articles / ([0-9] {4} /, represents a four-digit number, each digit is a 0 to any number of 9; extraction parameters, named form (? P < name> pattern) , such re_path ( Articles / (? P <year> [0-9] {}. 4) /, four digits extracted from a regular expression, each number is 0 to any number of named 9 year,

 

 

5, include usage

Matching can reduce duplication,

Urlpatterns = [

path(‘<page_slug>-<page_id>/’,include([

path(‘history/’,views.history),

path(‘edit/’,views.edit),

])]

 

You can also implement the mapping of distribution url

from django.urls import path,include

urlpatterns = [

path('admin/', admin.site.urls),
path('ant_test/',include('ant_test.urls'))
]

ant_test for the app name

 

Guess you like

Origin www.cnblogs.com/hanwenlin/p/11610794.html