the routing mechanism of URLs in Django2.0 is the process of associating URLs and their processing function relationships. The Django URL routing configuration specifies the global route file name root_urlconf the variable in the settings.py file. The Django route is written in the Urlpatterns list in the urls.py file, which consists of path () or Re_path () as an element. The Django URL routing process:1Django Lookup Global urlpatterns variable (urls.py)2each element is matched to the URL one by one in chronological order Urlpatterns3stops the lookup when the first match is found and executes the corresponding handler function based on the matching result. 4if no match or exception is found, Django makes error handling note: The Django route does not take into account the HTTP request, only route according to the URL, that is, as long as the URL is the same, regardless of the post, get and so on which request way to point to the same operation function. Path () in Urlpatterns handles string routing and Re_path handles regular expression routing. Its format: Urlpatterns=[Path (route,views. function name, additional parameters provided to the handler, represented as a dictionary, alias of the URL pattern), Re_path (regular expression, view. corresponding processing function)] where the regular expression can be seen as a pattern of strings. Django supports three kinds of expression route:1. Exact string format: articles/2017/an exact URL matches an operation function; The simplest form is appropriate for the response to a static URL; The URL string does not "/"Start with, but to"/"End2. Django Conversion format:< type: variable name >,articles/<int:year>/is a URL template, matching URL in which to get a batch of variables as parameters, is a common form, the purpose is to use the URL for parameter acquisition and delivery
description
| Convert format type | TD valign= "Top" width= "397" >
str |
Matches non-null characters except delimiter (/), default type <year> equivalent to <str:year> |
int |
matches 0 and positive integers |
slug |
Match A string of letters, numbers, bars, underscores, subsets of str |
uuid |
Matches formatted UUID, such as 075194d3-6885-417e-a8a8-6c931e272f00 |
path |
matches any non-empty string, including path delimiter, is complete |
3. Regular expression format: articles/(? P<year>[0-9]{4})/the use of regular expressions to enrich the syntax to express a class of URLs (rather than one);<>extract variables as parameters of the processing function, advanced usage; When using this method, you cannot use the path () function before, you must use the Re_path () function; All of the expressions are in str format and cannot be other types. Two forms: not extracting parameters, such as Re_path (articles/([0-9]{4}/, which represents four digits, each number is any number from 0 to 9; Extract the parameter, named form (? P<name>pattern), such as Re_path (articles/(? P<YEAR>[0-9]{4})/, the four-digit number extracted by the regular expression, each number is 0 to 9 any number is named year, when the view function path is large, you can use the include () usage for the de-duplication: Copy code urlpatterns=[Path ('<page_slug>-<page_id>/history/', views.history), Path ('<page_slug>-<page_id>/edit/', Views.edit),] copy code equivalent to: Copy code urlpatterns=[Path ('<page_slug>-<page_id>/', Include ([Path (' history/', views.history), Path (' Edit/', Views.edit),] Copy the code when the site is more functional, you can build a urls.py file in the function folder, the function module under the URL is all written in the file. However, the include method is used in global urls.py to implement URL map distribution. For example: The site has a forum module, the Forum module to build a urls.py file, the Forum related page URL is all written in this file, and then in the global urls.py file: Copy the Code fromDjango.urlsImportPath,includeurlpatterns=[Path ('admin/', Admin.site.urls), Path ('ant_test/', Include ('Ant_test.urls') ] In the urls.py file under the Forum module, write this: fromDjango.urlsImportPathurlpatterns=[Path ('news/', Views.news),] write the corresponding news function in views.py. The path (route,views. corresponding handler function) in the Django2.0 version is equivalent to the lower version of the URL (r'^route/$ ', views. corresponding processing function)
The routing mechanism of URLs in Django2.0