基于类的视图只是实现对请求的响应行为的类。它们提供了一种在同一路由节点处分别处理不同HTTP请求类型的方法。不是为每个路由节点支持的请求类型定义和装饰三个不同的处理函数,而是为路由节点分配基于类的视图。这非常类似于Tornado的类视图。
定义视图
基于类的视图应该继承自 HTTPMethodView
。然后就可以为每个 HTTP 请求类型实现类方法。如果收到的请求没有对应的方法,就会产生一个405: Method not allowed
的响应。
为了注册一个类视图到一个路由节点,可以使用app.add_route
方法。它的第一个参数应该是定义的类对as_view
方法的调用,第二个应该是URL节点。
可用的方法是get,post,put,patch和delete。 使用所有这些方法的类看起来如下所示:
from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import text
app = Sanic('some_name')
class SimpleView(HTTPMethodView):
def get(self, request):
return text('I am get method')
def post(self, request):
return text('I am post method')
def put(self, request):
return text('I am put method')
def patch(self, request):
return text('I am patch method')
def delete(self, request):
return text('I am delete method')
app.add_route(SimpleView.as_view(), '/')
对类视图的方法也可以使用async
语法:
from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import text
app = Sanic('some_name')
class SimpleAsyncView(HTTPMethodView):
async def get(self, request):
return text('I am async get method')
app.add_route(SimpleAsyncView.as_view(), '/')
URL参数
如果需要任何 URL 参数,可以像Sanic 路由中讲的那样,把它们当做类方法的参数。
class NameView(HTTPMethodView):
def get(self, request, name):
return text('Hello {}'.format(name))
app.add_route(NameView.as_view(), '/<name>')
修饰器
如果需要给类添加任何修饰器,可以设置decorators
类变量。它们会在调用as_view
是被应用。
class ViewWithDecorator(HTTPMethodView):
decorators = [some_decorator_here]
def get(self, request, name):
return text('Hello I have a decorator')
def post(self, request, name):
return text("Hello I also have a decorator")
app.add_route(ViewWithDecorator.as_view(), '/url')
但是,如果只是想装饰某些方法而不是所有的,可以这样操作:
class ViewWithSomeDecorator(HTTPMethodView):
@staticmethod
@some_decorator_here
def get(request, name):
return text("Hello I have a decorator")
def post(self, request, name):
return text("Hello I don't have any decorators")
URL建立
如果为一个HTTPMethodView
建立URL,类的名称就是传递给url_for
的路由节点。比如:
@app.route('/')
def index(request):
url = app.url_for('SpecialClassView')
return redirect(url)
class SpecialClassView(HTTPMethodView):
def get(self, request):
return text('Hello from the Special Class View!')
app.add_route(SpecialClassView.as_view(), '/special_class_view')
使用CompositionView
作为HTTPMethodView
的替代,我们可以使用CompositionView
把处理函数移到视图类的外面。
每个 HTTP 方法的处理函数定义在代码的其它地方,并通过CompositionView.add
方法添加到视图中。该方法的第一个参数是 HTTP 方法的列表(比如,['GET', 'POST']
),第二个参数是处理函数。下面的例子展示了CompositionView
用法:外部函数和内联的lambda函数:
from sanic import Sanic
from sanic.views import CompositionView
from sanic.response import text
app = Sanic(__name__)
def get_handler(request):
return text('I am a get method')
view = CompositionView()
view.add(['GET'], get_handler)
view.add(['POST', 'PUT'], lambda request: text('I am a post/put method'))
# Use the new view to handle requests to the base URL
app.add_route(view, '/')
需要注意的是,目前不能使用url_for
为CompositionView
创建URL。

我的公众号:猿人学 Python 上会分享更多心得体会,敬请关注。
***版权申明:若没有特殊说明,文章皆是猿人学 yuanrenxue.con 原创,没有猿人学授权,请勿以任何形式转载。***
说点什么吧...