術語 “視圖配置 “是指將視圖的可調(diào)用性(一個函數(shù)、方法或一個類)與路由配置的信息聯(lián)系起來的機制。Pyramid為給定的URL模式找到最佳的可調(diào)用性。
There are three ways to configure a view ?
這是最簡單的配置視圖的方法,通過調(diào)用 Configurator 對象的 add_view() 方法來實現(xiàn)。
This method uses the following arguments ?
在下面的例子中,定義了兩個函數(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裝飾器來將配置好的路由與一個函數(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() 是一個類裝飾器。如果你必須將一個類中的方法作為視圖添加一些通用參數(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
更多建議: