URL Definition :
urls.py ----- Responsible for the URL Schema mapping to application .URL yes Django After receiving the user's request , According to the user's request URL Address and urls.py The mapping relationship configured in , Identify a with the right URL Which piece of logic code is called to execute the corresponding view function or view class , Finally, the view returns the data that the client wants to receive .
Django The process of route resolution in is mainly :
There are two main types of routing :
You can also define all the routing information of the function in the main routing file , No longer set in subapplication urls.py In file ;
Route resolution order :
The search order of routes is from top to bottom , therefore It is possible to shield the upper route from the lower route , Bring unexpected results .
Therefore, when setting the route, you should :
1. Note the order in which the routes are defined , Avoid shielding effects .
2. When defining a sub route , Write the regular expression completely , Add a close .
Slash at the end of the path / Explanation :
Django When routing is defined in , Usually with a slash / ending , The advantage is that users do not access with slashes / At the end of the same path ,Django Will redirect the user to a slash / At the end of the path , Instead of going back 404 non-existent .
Custom expressions match : Converter type Type specification
<int:number>
take number Convert to integer , matching 0 Or any positive integer
<str:name>
take name Convert to floating point , Match except / Non empty characters other than
<slug:path1>
Match the letter 、 Numbers 、 String of underscores
<uuid:path2>
Match formatted UUID Unique identification number
<path:path3>
Matches any non-null characters , Include path delimiters ,? With the exception of
Other matters needing attention :
urlpatterns The elements in () match the regular expression from top to bottom in writing order , Once the match is successful, it will not continue .
If you want to rush URL Captures a value in , Just put a pair of parentheses around it ( Assign groups )
There is no need to add a leading backslash ( That's the one written at the front of the regular /), Because of every url There are . for example : Should be ^index instead of ^/index. Before each regular expression "r" Optional, but it is recommended to add idex$ What does it end with , What does it start with , Strictly restrict the path
問題遇到的現象和發生背景macOS Monterey12.3
One 、 Subject requirements : C