99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Pyramid 響應(yīng)對(duì)象

2023-03-30 17:39 更新

響應(yīng)類定義在pyramid.response模塊中。該類的一個(gè)對(duì)象由視圖的可調(diào)用性返回。

from pyramid.response import Response
def hell(request):
   return Response("Hello World")

響應(yīng)對(duì)象包含一個(gè)狀態(tài)代碼(默認(rèn)是200 OK),一個(gè)響應(yīng)頭的列表和響應(yīng)體。大多數(shù)HTTP響應(yīng)頭都可以作為屬性使用。以下屬性對(duì)響應(yīng)對(duì)象是可用的

  • response.content_type – 內(nèi)容類型是一個(gè)字符串,如 – response.content_type = ‘text/html’。
  • response.charset – 它也通知 response.text 中的編碼 。
  • response.set_cookie – 這個(gè)屬性用來設(shè)置一個(gè)cookie。需要給出的參數(shù)是name、value和max_age。
  • response.delete_cookie – 從客戶端刪除一個(gè)cookie。有效地,它將max_age設(shè)置為0,并將cookie的值設(shè)置為”。

pyramid.httpexceptions 模塊定義了處理錯(cuò)誤響應(yīng)的類,如404 Not Found。這些類實(shí)際上是 響應(yīng) 類的子類。一個(gè)這樣的類是 “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屬性來將客戶端重定向到另一個(gè)路由。例如 –

view_config(route_name='add', request_method='POST')
def add(request):
   #add a new object
   return HTTPFound(location='http://localhost:6543/')


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)