
- Python Pyramid 教程
- Python Pyramid - 首頁
- Python Pyramid - 概述
- Pyramid - 環境設定
- Python Pyramid - Hello World
- Pyramid - 應用配置
- Python Pyramid - URL 路由
- Python Pyramid - 檢視配置
- Python Pyramid - 路由字首
- Python Pyramid - 模板
- Pyramid - HTML 表單模板
- Python Pyramid - 靜態資源
- Python Pyramid - 請求物件
- Python Pyramid - 響應物件
- Python Pyramid - 會話
- Python Pyramid - 事件
- Python Pyramid - 訊息閃現
- Pyramid - 使用 SQLAlchemy
- Python Pyramid - Cookiecutter
- Python Pyramid - 建立專案
- Python Pyramid - 專案結構
- Python Pyramid - 包結構
- 手動建立專案
- 命令列 Pyramid
- Python Pyramid - 測試
- Python Pyramid - 日誌記錄
- Python Pyramid - 安全性
- Python Pyramid - 部署
- Python Pyramid 有用資源
- Python Pyramid - 快速指南
- Python Pyramid - 有用資源
- Python Pyramid - 討論
Python Pyramid - URL 路由
在 MVC 架構出現之前,Web 應用使用將使用者在瀏覽器中輸入的 URL 對映到程式檔案,該程式檔案的輸出被渲染為 HTML 作為響應返回到瀏覽器的機制。Pyramid 框架使用路由機制,其中 URL 的端點與應用登錄檔中註冊的不同 URL 模式匹配,呼叫其對映的檢視並呈現響應。
一個典型的 URL 包含三個部分:協議(例如 http:// 或 https://)後跟 IP 地址或主機名。主機名之後第一個 / 後面的 URL 剩餘部分稱為路徑或端點。

端點後跟一個或多個可變部分構成路由。可變部分識別符號用花括號括起來。例如,對於上述 URL,路由為 /blog/{id}
WSGI 應用充當路由器。它根據路由對映中存在的 URL 模式檢查傳入請求。如果找到匹配項,則執行其關聯的檢視可呼叫項並返回響應。
路由配置
透過呼叫 Configurator 物件的 add_route() 方法,將新路由新增到應用中。路由有一個名稱,用作 URL 生成時使用的識別符號,以及一個模式,該模式旨在與 URL 的 PATH_INFO 部分(方案和埠之後的部分,例如 URL http://example.com/blog/1 中的 /blog/1)匹配。
如前所述,add_route() 方法的 pattern 引數可以有一個或多個用花括號括起來並用 / 分隔的佔位符識別符號。以下語句將 'index' 作為分配給 '/{name}/{age}' 模式的路由名稱。
config.add_route('index', '/{name}/{age}')
要將檢視可呼叫項與該路由關聯,我們使用 add_view() 函式,如下所示:
config.add_view(index, route_name='index')
index() 函式應可用於將路由與其匹配。
def index(request): return Response('Root Configuration Example')
示例
我們將這些語句放在下面的程式中:
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response def index(request): return Response('Root Configuration Example') if __name__ == '__main__': with Configurator() as config: config.add_route('index', '/{name}/{age}') config.add_view(index, route_name='index') app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
輸出
執行以上程式碼並在瀏覽器中訪問 https://:6543/Ravi/21。由於 URL 的 PATH_INFO 與 index 路由匹配,因此顯示以下輸出:

路由配置中使用的模式通常以正斜槓 (/) 字元開頭。模式段(模式中 / 字元之間的單個專案)可以是文字字串,也可以是佔位符標記(例如 {name}),或者兩者組合。替換標記不需要以 / 字元開頭。
以下是一些路由模式示例
/student/{name}/{marks} /{id}/student/{name}/{marks} /customer/{id}/item/{itemno} /{name}/{age}
佔位符識別符號必須是有效的 Python 識別符號。因此,它必須以大寫或小寫 ASCII 字母或下劃線開頭,並且只能包含大寫或小寫 ASCII 字母、下劃線和數字。
路由匹配
當傳入請求與特定路由配置關聯的 URL 模式匹配時,名為 matchdict 的字典物件作為請求物件的屬性新增。
request.matchdict 包含與模式元素中的替換模式匹配的值。matchdict 中的鍵是字串,而它們的值是 Unicode 物件。
在上一個示例中,將 index() 檢視函式更改為以下內容:
def index(request): return Response(str(request.matchdict))
瀏覽器以 dict 物件的形式顯示路徑引數。

當請求與路由模式匹配時,傳遞給檢視函式的請求物件還包括 matched_route 屬性。匹配路由的名稱可以從其 name 屬性獲取。
示例
在以下示例中,我們使用 @view.config() 裝飾器定義了兩個檢視函式 student_view() 和 book_view()。
應用的登錄檔配置為具有兩個相應的路由 - 'student' 對映到 '/student/{name}/{age}' 模式,'book' 對映到 '/book/{title}/{price}' 模式。我們呼叫 configurator 物件的 scan() 方法來新增檢視。
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response from pyramid.view import view_config @view_config(route_name='student') def student_view(request): return Response(str(request.matchdict)) @view_config(route_name='book') def book_view(request): title=request.matchdict['title'] price=request.matchdict['price'] return Response('Title: {}, Price: {}'.format(title,price)) if __name__ == '__main__': with Configurator() as config: config.add_route('student', '/student/{name}/{age}') config.add_route('book', '/book/{title}/{price}') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
輸出
當瀏覽器給出 https://:6543/student/Ravi/21 URL 時,輸出為
{'name': 'Ravi', 'age': '21'}
如果輸入的 URL 為 https://:6543/book/Python/300,則輸出為
Title: Python, Price: 300