Pyramid 视图配置

2023-03-30 17:25 更新

术语 “视图配置 “是指将视图的可调用性(一个函数、方法或一个类)与路由配置的信息联系起来的机制。Pyramid为给定的URL模式找到最佳的可调用性。

There are three ways to configure a view −

  • 使用add_view()方法
  • 使用@view_config()装饰器
  • 使用@view_defaults()类装饰器

使用add_view()方法

这是最简单的配置视图的方法,通过调用 Configurator 对象的 add_view() 方法来实现。

This method uses the following arguments −

  • name – 视图名称,需要与这个视图可调用性相匹配。如果没有提供name,就会使用空字符串(意味着默认视图)。
  • context – 这个资源必须是Python类的一个对象,以便这个视图被找到和调用。如果没有提供context,将使用None值,它可以匹配任何资源。
  • route_name – 这个值必须与路由配置声明的名称相匹配,在这个视图被调用之前必须匹配。如果提供了route_name,视图的可调用性将只在命名的路由匹配后被调用。
  • request_type – 请求必须提供的接口,以便该视图被找到和调用。
  • request_method – 代表HTTP REQUEST_METHOD的字符串(如 “GET”、”POST”、”PUT”、”DELETE”、”HEAD “或 “OPTIONS”)或包含一个或多个这些字符串的元组。只有当请求的方法属性与提供的值相匹配时,该视图才会被调用。
  • request_param – 这个参数可以是任何字符串或字符串的序列。只有当request.params字典中有一个与提供的值相匹配的键时,该视图才会被调用。

例子

在下面的例子中,定义了两个函数 getview() 和 postview() ,并与两个同名的路由关联。这些函数只是返回它们被调用的 HTTP 方法的名称。

当使用GET方法请求URL /get 时,getview()函数被调用。同样地,当用POST方法请求 /post 路径id时,postview()函数被执行。

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请求可以通过使用网络浏览器作为HTTP客户端来发送,但不可能使用它来发送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 参数可以是一个或多个HTTP方法的列表。让我们修改上述程序,定义一个单一的 oneview() 函数,确定导致其执行的HTTP方法。

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

这个函数被注册在应用程序的配置中,用于所有的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装饰器来将配置好的路由与一个函数、一个方法甚至是一个可调用的类联系起来,而不是强制性地添加视图。

例子

正如声明式配置部分所描述的,一个注册的路由可以与一个函数相关联,就像下面的例子一样

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

请注意,只有在调用scan()方法后,视图才会被添加到应用配置中。虽然消除了必须添加视图的需要,但性能可能会稍微慢一些。

输出

view_config()装饰器也可以给出和add_view()方法一样的参数。所有的参数都可以被省略。

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

在这种情况下,该函数将以任何路由名称、任何请求方法或参数被注册。

view_config装饰器就放在可调用视图函数的定义之前,如上面的例子。它也可以放在一个类的顶部,如果它要被用作视图的可调用。这样的类必须有一个call()方法。

在下面的Pyramid应用程序代码中, MyView 类被用作可调用的,并被 @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()

注意,我们可以通过显式调用add_view()方法来添加视图,而不是扫描视图配置。

例子

如果一个类中的方法必须与不同的路由相关联,那么应该在每个方法上面使用单独的@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() 是一个类装饰器。如果你必须将一个类中的方法作为视图添加一些通用参数和一些特殊参数,通用参数可以在类的顶部的 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()

输出

向服务器发送不同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


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号