
- 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 - 響應物件
Response 類定義在 pyramid.response 模組中。此類的物件由檢視可呼叫物件返回。
from pyramid.response import Response def hell(request): return Response("Hello World")
響應物件包含狀態程式碼(預設值為 200 OK)、響應頭列表和響應正文。大多數 HTTP 響應頭都可以作為屬性訪問。Response 物件可用的屬性如下:
response.content_type − 內容型別是一個字串,例如 – response.content_type = 'text/html'。
response.charset − 它還在 response.text 中告知編碼。
response.set_cookie − 此屬性用於設定cookie。需要提供的引數是名稱、值和 max_age。
response.delete_cookie − 從客戶端刪除 cookie。有效地,它將 max_age 設定為 0,並將 cookie 值設定為 ''。
pyramid.httpexceptions 模組定義了用於處理錯誤響應(例如 404 未找到)的類。這些類實際上是 Response 類的子類。“pyramid.httpexceptions.HTTPNotFound”就是一個這樣的類。其典型用法如下:
from pyramid.httpexceptions import HTTPNotFound from pyramid.config import view_config @view_config(route='Hello') def hello(request): response = HTTPNotFound("There is no such route defined") return response
我們可以使用 Response 類的 location 屬性將客戶端重定向到另一個路由。例如:
view_config(route_name='add', request_method='POST') def add(request): #add a new object return HTTPFound(location='https://:6543/')
廣告