程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Technical document 2: URL route resolution in Django

編輯:Python

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 :

  1. The client sends the access request ;
  2. The server accepts the request from the client , Disassembly request ;
  3. The server gives priority to matching... Under the project directory URL, Then match... In specific applications URL;
  4. use sb.'s judgement , If the matching is successful, execute the corresponding view ; If the match is not successful, it returns 404;
  5. The client accepts the response and renders the execution ;

There are two main types of routing :

  1. Defined in the directory with the same name of the project urls.py In the file of , The document is Django Analyze the entry of the route , Also called total routing ;
  2. Each sub application in order to maintain relative independence , Define your own... In each sub application urls.py To save the route of the application , Also called sub 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

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved