Flask 中基于类的(通用)视图
使用 Flask Web 框架编写通用视图的最佳方法是什么?
@app.route 装饰器支持可调用类吗?或者我是否以完全错误的方式思考这个问题?
任何帮助或建议将不胜感激!
What's the best way to write generic views using the Flask web framework?
Does the @app.route decorator support callable classes? Or am I thinking about this in entirely the wrong fashion?
Any help or advice would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Python 2.6 开始,您也可以将装饰器应用于类。可调用类没有内置模式,因为实现它们的方法太多,但本质上,技巧是重写类上的
__call__
并拥有一个实例化该类的包装装饰器。我本来计划有一个基于类的视图扩展,但到目前为止没有人想出一些好的行为:)
Starting with Python 2.6 you can apply the decorators to classes as well. There is no builtin pattern for callable classes because there are too many ways to implement them, but essentially the trick would be to override
__call__
on the class and to have a wrapper decorator that instanciates the class.I was planning on having a class-based-view extension but so far nobody came up with some good behavior for it :)