基于类的视图

基于类的视图只是实现对请求的响应行为的类。它们提供了在同一个端点上划分不同HTTP请求类型的处理方式。相当与定义和装饰了三种不同的处理程序,每端支持一种请求类型,端点可以被分配一个基于类的视图。

定义视图

一个基于类的视图应该继承 HTTPMethodView。你可以为每个你想要支持的 HTTP 请求类型执行类方法。如果请求已经接收但是没有定义方法,一个 405: Method not allowed 响应就会生成。

要注册一个基于类的视图到端,app.add_route 方法是有用的。第一个参数应该是被定义带 as_view 方法的类,第二个参数应该是 URL 端。

有效的方法有 get, post, put, patchdelete。一个使用所有方法的类看起来如下。

  1. from sanic import Sanic
  2. from sanic.views import HTTPMethodView
  3. from sanic.response import text
  4. app = Sanic('some_name')
  5. class SimpleView(HTTPMethodView):
  6. def get(self, request):
  7. return text('I am get method')
  8. def post(self, request):
  9. return text('I am post method')
  10. def put(self, request):
  11. return text('I am put method')
  12. def patch(self, request):
  13. return text('I am patch method')
  14. def delete(self, request):
  15. return text('I am delete method')
  16. app.add_route(SimpleView.as_view(), '/')

你也可以使用 async 语法。

  1. from sanic import Sanic
  2. from sanic.views import HTTPMethodView
  3. from sanic.response import text
  4. app = Sanic('some_name')
  5. class SimpleAsyncView(HTTPMethodView):
  6. async def get(self, request):
  7. return text('I am async get method')
  8. app.add_route(SimpleAsyncView.as_view(), '/')

URL 参数

如果需要路由指南中讨论的任何URL参数,请将其包括在方法定义中。

  1. class NameView(HTTPMethodView):
  2. def get(self, request, name):
  3. return text('Hello {}'.format(name))
  4. app.add_route(NameView.as_view(), '/<name>')

装饰器

如果你想要添加任何装饰器到类,你可以设置 decorators 类的变量。这些会在 as_view 被调用时应用到类。

  1. class ViewWithDecorator(HTTPMethodView):
  2. decorators = [some_decorator_here]
  3. def get(self, request, name):
  4. return text('Hello I have a decorator')
  5. def post(self, request, name):
  6. return text("Hello I also have a decorator")
  7. app.add_route(ViewWithDecorator.as_view(), '/url')

但是如果你只想装饰一些函数而不是所有,你可以操作如下:

  1. class ViewWithSomeDecorator(HTTPMethodView):
  2. @staticmethod
  3. @some_decorator_here
  4. def get(request, name):
  5. return text("Hello I have a decorator")
  6. def post(self, request, name):
  7. return text("Hello I don't have any decorators")

URL 构建

如果你希望为 HTTPMethodView 构建 URL,记住类名将成为你传入 url_for 的端点。例如:

  1. @app.route('/')
  2. def index(request):
  3. url = app.url_for('SpecialClassView')
  4. return redirect(url)
  5. class SpecialClassView(HTTPMethodView):
  6. def get(self, request):
  7. return text('Hello from the Special Class View!')
  8. app.add_route(SpecialClassView.as_view(), '/special_class_view')

使用 CompositionView

作为 HTTPMethodView 的替代方案,你可用 CompositionView 在视图类之外移动处理函数。

每个支持的 HTTP 方法的处理程序都在源文件的其他地方定义了,然后使用 CompositionView.add 方法添加到视图。第一个参数是一个要处理的 HTTP 方法列表 (e.g. ['GET', 'POST']),第二个是处理程序。下面的例子展示了带两个外部处理程序和一个内部匿名函数的 CompositionView 的用法:

  1. from sanic import Sanic
  2. from sanic.views import CompositionView
  3. from sanic.response import text
  4. app = Sanic(__name__)
  5. def get_handler(request):
  6. return text('I am a get method')
  7. view = CompositionView()
  8. view.add(['GET'], get_handler)
  9. view.add(['POST', 'PUT'], lambda request: text('I am a post/put method'))
  10. # Use the new view to handle requests to the base URL
  11. app.add_route(view, '/')

注意:当前你不能通过 url_for 为 CompositionView 建立 URL。