Pyramid 視圖配置

2023-03-30 17:25 更新

術語 “視圖配置 “是指將視圖的可調(diào)用性(一個函數(shù)、方法或一個類)與路由配置的信息聯(lián)系起來的機制。Pyramid為給定的URL模式找到最佳的可調(diào)用性。

There are three ways to configure a view ?

  • 使用add_view()方法
  • 使用@view_config()裝飾器
  • 使用@view_defaults()類裝飾器

使用add_view()方法

這是最簡單的配置視圖的方法,通過調(diào)用 Configurator 對象的 add_view() 方法來實現(xiàn)。

This method uses the following arguments ?

  • name – 視圖名稱,需要與這個視圖可調(diào)用性相匹配。如果沒有提供name,就會使用空字符串(意味著默認視圖)。
  • context – 這個資源必須是Python類的一個對象,以便這個視圖被找到和調(diào)用。如果沒有提供context,將使用None值,它可以匹配任何資源。
  • route_name – 這個值必須與路由配置聲明的名稱相匹配,在這個視圖被調(diào)用之前必須匹配。如果提供了route_name,視圖的可調(diào)用性將只在命名的路由匹配后被調(diào)用。
  • request_type – 請求必須提供的接口,以便該視圖被找到和調(diào)用。
  • request_method – 代表HTTP REQUEST_METHOD的字符串(如 “GET”、”POST”、”PUT”、”DELETE”、”HEAD “或 “OPTIONS”)或包含一個或多個這些字符串的元組。只有當請求的方法屬性與提供的值相匹配時,該視圖才會被調(diào)用。
  • request_param – 這個參數(shù)可以是任何字符串或字符串的序列。只有當request.params字典中有一個與提供的值相匹配的鍵時,該視圖才會被調(diào)用。

例子

在下面的例子中,定義了兩個函數(shù) getview() 和 postview() ,并與兩個同名的路由關聯(lián)。這些函數(shù)只是返回它們被調(diào)用的 HTTP 方法的名稱。

當使用GET方法請求URL /get 時,getview()函數(shù)被調(diào)用。同樣地,當用POST方法請求 /post 路徑id時,postview()函數(shù)被執(zhí)行。

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def getview(request):
   ret=request.method
   return Response('Method: {}'.format(ret))
def postview(request):
   ret=request.method
   return Response('Method: {}'.format(ret))

if __name__ == '__main__':
   with Configurator() as config:
      config.add_route('getview', '/get')
      config.add_route('postview', '/post')
      config.add_view(getview, route_name='getview',request_method='GET')
      config.add_view(postview,route_name='postview', request_method='POST')
      app = config.make_wsgi_app()
      server = make_server('0.0.0.0', 6543, app)
      server.serve_forever()

雖然GET請求可以通過使用網(wǎng)絡瀏覽器作為HTTP客戶端來發(fā)送,但不可能使用它來發(fā)送POST請求。因此,我們使用CURL命令行工具。

C:\Users\Acer>curl localhost:6543/get
Method: GET
C:\Users\Acer>curl -d "param1=value1" -H "Content-Type: application/json" -X POST http://localhost:6543/post
Method: POST

如前所述, request_method 參數(shù)可以是一個或多個HTTP方法的列表。讓我們修改上述程序,定義一個單一的 oneview() 函數(shù),確定導致其執(zhí)行的HTTP方法。

def oneview(request):
   ret=request.method
   return Response('Method: {}'.format(ret))

這個函數(shù)被注冊在應用程序的配置中,用于所有的HTTP方法。

config.add_route('oneview', '/view')
config.add_view(oneview, route_name='oneview',
   request_method=['GET','POST', 'PUT', 'DELETE'])

輸出

CURL的輸出如下圖所示

C:\Users\Acer>curl localhost:6543/view
Method: GET
C:\Users\Acer>curl -d "param1=value1" -H "Content-Type: application/json" -X POST http://localhost:6543/view
Method: POST
C:\Users\Acer>curl -d "param1=value1" -H "Content-Type: application/json" -X PUT http://localhost:6543/view
Method: PUT
C:\Users\Acer>curl -X DELETE http://localhost:6543/view
Method: DELETE

使用@view_config()裝飾器

可以使用@view_config裝飾器來將配置好的路由與一個函數(shù)、一個方法甚至是一個可調(diào)用的類聯(lián)系起來,而不是強制性地添加視圖。

例子

正如聲明式配置部分所描述的,一個注冊的路由可以與一個函數(shù)相關聯(lián),就像下面的例子一樣

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')
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()

請注意,只有在調(diào)用scan()方法后,視圖才會被添加到應用配置中。雖然消除了必須添加視圖的需要,但性能可能會稍微慢一些。

輸出

view_config()裝飾器也可以給出和add_view()方法一樣的參數(shù)。所有的參數(shù)都可以被省略。

@view_config()
def hello_world(request):
   return Response('Hello World!')

在這種情況下,該函數(shù)將以任何路由名稱、任何請求方法或參數(shù)被注冊。

view_config裝飾器就放在可調(diào)用視圖函數(shù)的定義之前,如上面的例子。它也可以放在一個類的頂部,如果它要被用作視圖的可調(diào)用。這樣的類必須有一個call()方法。

在下面的Pyramid應用程序代碼中, MyView 類被用作可調(diào)用的,并被 @view_config 裝飾器所裝飾。

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')
class MyView(object):
   def __init__(self, request):
      self.request = request

   def __call__(self):
      return Response('hello World')

if __name__ == '__main__':
   with Configurator() as config:
      config.add_route('hello', '/')
      #config.add_view(MyView, route_name='hello')
      config.scan()
      app = config.make_wsgi_app()
   server = make_server('0.0.0.0', 6543, app)
   server.serve_forever()

注意,我們可以通過顯式調(diào)用add_view()方法來添加視圖,而不是掃描視圖配置。

例子

如果一個類中的方法必須與不同的路由相關聯(lián),那么應該在每個方法上面使用單獨的@view_config(),就像下面的例子那樣。這里,我們有兩個方法被綁定到兩個獨立的路由。

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import 

class MyView(object):
   def __init__(self, request):
      self.request = request

   @view_config(route_name='getview', request_method='GET')
   def getview(self):
      return Response('hello GET')
   @view_config(route_name='postview', request_method='POST')
   def postview(self):
      return Response('hello POST')

if __name__ == '__main__':
   with Configurator() as config:
      config.add_route('getview', '/get')
      config.add_route('postview', '/post')
      config.scan()
      app = config.make_wsgi_app()
   server = make_server('0.0.0.0', 6543, app)
   server.serve_forever()

輸出

下面是CURL命令的輸出:

C:\Users\Acer>curl localhost:6543/get
hello GET
C:\Users\Acer>curl -d "param1=value1" -H "Content-Type: application/json" -X POST http://localhost:6543/post
hello POST

使用@view_defaults()裝飾器

view_defaults() 是一個類裝飾器。如果你必須將一個類中的方法作為視圖添加一些通用參數(shù)和一些特殊參數(shù),通用參數(shù)可以在類的頂部的 view_defaults() 裝飾器中指定,通過在每個方法之前單獨的 view_config() 進行配置。

例子

在下面的代碼中,我們有不同的方法響應同一個路由,但有不同的 request_method。 因此,我們將路由名稱定義為默認,并在每個視圖配置中指定 request_method 。

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.view import view_defaults

@view_defaults(route_name='myview')
class MyView(object):
   def __init__(self, request):
      self.request = request

   @view_config( request_method='GET')
   def getview(self):
      return Response('hello GET')
   @view_config(request_method='POST')
   def postview(self):
      return Response('hello POST')
   @view_config(request_method='PUT')
   def putview(self):
      return Response('hello PUT')
   @view_config(request_method='DELETE')
   def delview(self):
      return Response('hello DELETE')

if __name__ == '__main__':
   with Configurator() as config:
      config.add_route('myview', '/view')
      config.scan()
      app = config.make_wsgi_app()
   server = make_server('0.0.0.0', 6543, app)
   server.serve_forever()

輸出

向服務器發(fā)送不同HTTP請求的CURL命令如下 ?

C:\Users\Acer>curl localhost:6543/view
hello GET
C:\Users\Acer>curl -d "param1=value1" -H "Content-Type: application/json" -X POST http://localhost:6543/view
hello POST
C:\Users\Acer>curl -d "param1=value1" -H "Content-Type: application/json" -X PUT http://localhost:6543/view
hello PUT
C:\Users\Acer>curl -X DELETE http://localhost:6543/view
hello DELETE


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號