術(shù)語(yǔ) “視圖配置 “是指將視圖的可調(diào)用性(一個(gè)函數(shù)、方法或一個(gè)類(lèi))與路由配置的信息聯(lián)系起來(lái)的機(jī)制。Pyramid為給定的URL模式找到最佳的可調(diào)用性。
There are three ways to configure a view ?
這是最簡(jiǎn)單的配置視圖的方法,通過(guò)調(diào)用 Configurator 對(duì)象的 add_view() 方法來(lái)實(shí)現(xiàn)。
This method uses the following arguments ?
在下面的例子中,定義了兩個(gè)函數(shù) getview() 和 postview() ,并與兩個(gè)同名的路由關(guān)聯(lián)。這些函數(shù)只是返回它們被調(diào)用的 HTTP 方法的名稱(chēng)。
當(dāng)使用GET方法請(qǐng)求URL /get 時(shí),getview()函數(shù)被調(diào)用。同樣地,當(dāng)用POST方法請(qǐng)求 /post 路徑id時(shí),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請(qǐng)求可以通過(guò)使用網(wǎng)絡(luò)瀏覽器作為HTTP客戶(hù)端來(lái)發(fā)送,但不可能使用它來(lái)發(fā)送POST請(qǐng)求。因此,我們使用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ù)可以是一個(gè)或多個(gè)HTTP方法的列表。讓我們修改上述程序,定義一個(gè)單一的 oneview() 函數(shù),確定導(dǎo)致其執(zhí)行的HTTP方法。
def oneview(request):
ret=request.method
return Response('Method: {}'.format(ret))
這個(gè)函數(shù)被注冊(cè)在應(yīng)用程序的配置中,用于所有的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裝飾器來(lái)將配置好的路由與一個(gè)函數(shù)、一個(gè)方法甚至是一個(gè)可調(diào)用的類(lèi)聯(lián)系起來(lái),而不是強(qiáng)制性地添加視圖。
正如聲明式配置部分所描述的,一個(gè)注冊(cè)的路由可以與一個(gè)函數(shù)相關(guān)聯(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()
請(qǐng)注意,只有在調(diào)用scan()方法后,視圖才會(huì)被添加到應(yīng)用配置中。雖然消除了必須添加視圖的需要,但性能可能會(huì)稍微慢一些。
view_config()裝飾器也可以給出和add_view()方法一樣的參數(shù)。所有的參數(shù)都可以被省略。
@view_config()
def hello_world(request):
return Response('Hello World!')
在這種情況下,該函數(shù)將以任何路由名稱(chēng)、任何請(qǐng)求方法或參數(shù)被注冊(cè)。
view_config裝飾器就放在可調(diào)用視圖函數(shù)的定義之前,如上面的例子。它也可以放在一個(gè)類(lèi)的頂部,如果它要被用作視圖的可調(diào)用。這樣的類(lèi)必須有一個(gè)call()方法。
在下面的Pyramid應(yīng)用程序代碼中, MyView 類(lèi)被用作可調(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()
注意,我們可以通過(guò)顯式調(diào)用add_view()方法來(lái)添加視圖,而不是掃描視圖配置。
如果一個(gè)類(lèi)中的方法必須與不同的路由相關(guān)聯(lián),那么應(yīng)該在每個(gè)方法上面使用單獨(dú)的@view_config(),就像下面的例子那樣。這里,我們有兩個(gè)方法被綁定到兩個(gè)獨(dú)立的路由。
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() 是一個(gè)類(lèi)裝飾器。如果你必須將一個(gè)類(lèi)中的方法作為視圖添加一些通用參數(shù)和一些特殊參數(shù),通用參數(shù)可以在類(lèi)的頂部的 view_defaults() 裝飾器中指定,通過(guò)在每個(gè)方法之前單獨(dú)的 view_config() 進(jìn)行配置。
在下面的代碼中,我們有不同的方法響應(yīng)同一個(gè)路由,但有不同的 request_method。 因此,我們將路由名稱(chēng)定義為默認(rèn),并在每個(gè)視圖配置中指定 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()
向服務(wù)器發(fā)送不同HTTP請(qǐng)求的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
更多建議: