
- 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 - 應用配置
Pyramid 應用物件具有一個應用登錄檔,該登錄檔儲存檢視函式與路由的對映以及其他特定於應用程式的元件註冊。Configurator 類用於構建應用程式登錄檔。
Configurator 生命週期由上下文管理器管理,該管理器返回一個應用程式物件。
with Configurator(settings=settings) as config: #configuration methods app = config.make_wsgi_app()
Configurator 類定義了以下重要的用於自定義應用程式的方法:
add_route()
此方法註冊用於 URL 排程的路由。使用以下引數:
name − 第一個必需的位置引數必須是路由的唯一名稱。在註冊檢視或生成 URL 時,使用名稱來標識路由。
pattern − 第二個必需的位置引數是一個字串,表示 URL 路徑,該路徑可以選擇包含用於從 URL 解析變數資料的變數佔位符。佔位符用花括號括起來。例如,“/students/{id}”。
request_method − 值可以是“GET”、“POST”、“HEAD”、“DELETE”、“PUT”之一。只有此型別的請求才會與路由匹配。
add_view()
此方法嚮應用程式登錄檔新增檢視配置。它將檢視函式繫結到配置中存在的route_name。所需的引數為:
view − 檢視函式的名稱。
route_name − 一個字串,必須與路由配置宣告的名稱匹配。
request_method − 表示 HTTP REQUEST_METHOD 的字串(例如“GET”、“POST”、“PUT”、“DELETE”、“HEAD”或“OPTIONS”),或包含一個或多個這些字串的元組。
add_static_view()
此方法新增用於呈現靜態資源(如影像和 CSS 檔案)的view,並使用以下引數:
name − 此引數是一個字串,表示應用程式相關的本地 URL 字首或完整的 URL。
Path − 此引數表示磁碟上靜態檔案所在的路徑。其值可以是絕對路徑或包相關的路徑。
此方法依次呼叫 Configurator 物件的add_route() 方法。
add_notfound_view()
此方法新增一個檢視,當找不到與當前請求匹配的檢視時執行此檢視。以下程式碼顯示了一個示例:
from pyramid.config import Configurator from pyramid.response import Response def notfound(request): return Response('Not Found', status='404 Not Found') config.add_notfound_view(notfound)
add_forbidden_view()
配置應用程式登錄檔,以便定義一個在引發 HTTPForbidden 異常時執行的檢視。引數列表包含對返回 403 狀態響應的函式的引用。如果未提供引數,則登錄檔會新增default_exceptionresponse_view()。
add_exception_view()
此方法會將異常檢視函式新增到配置中,用於指定的異常。
make_wsgi_app()
此方法返回一個 Pyramid WSGI 應用程式物件。
scan()
這是註冊檢視的包裝器。它匯入所有應用程式模組,查詢 @view_config 裝飾器。
對於每一個,它都會使用相同的關鍵字引數呼叫 config.add_view(view)。呼叫 scan() 函式會對包及其所有子包進行掃描,查詢所有裝飾器。
執行應用程式登錄檔配置的典型語句序列如下面的程式碼片段所示:
from pyramid.config import Configurator with Configurator() as config: config.add_route('hello', '/') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app()
這種應用程式配置方法稱為命令式配置。Pyramid 提供了另一種配置方法,稱為宣告式配置。
宣告式配置
有時,用命令式程式碼進行配置會很困難,尤其是在應用程式程式碼分散在許多檔案中的情況下。宣告式配置是一種便捷的方法。pyramid.view 模型定義了view_config——一個函式、類或方法裝飾器——它允許在檢視函式本身的定義附近進行檢視註冊。
向@view_config()裝飾器提供了兩個重要的引數。它們是route_name和request_method。它們與 Configurator 類的add_route()方法中的解釋相同。緊隨其後的函式進行裝飾,以便將其繫結到新增到應用程式物件登錄檔的路由。
以下是hello_world()檢視函式的宣告式配置示例:
from pyramid.response import Response from pyramid.view import view_config @view_config(route_name='hello', request_method='GET') def hello_world(request): return Response('Hello World!')
view_config 裝飾器向 hello_world() 函式新增一個屬性,使其稍後可供掃描程式查詢。
示例
配置裝飾和掃描呼叫的組合統稱為宣告式配置。以下程式碼使用宣告式方法配置應用程式登錄檔。
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='hello', request_method='GET') def hello_world(request): return Response('Hello World!') if __name__ == '__main__': with Configurator() as config: config.add_route('hello', '/') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
掃描程式將引數轉換為對 pyramid.config.Configurator.add_view() 方法的呼叫,因此該操作等效於以下語句:
config.add_view(hello_world, route_name='hello', request_method='GET')
輸出
執行上述程式後,WSGI 伺服器啟動。當瀏覽器訪問連結 https://:6543/ 時,“Hello World”訊息將像以前一樣呈現。
