简体   繁体   English

Flask 中基于类的(通用)视图

[英]Class-based (generic) views in Flask

What's the best way to write generic views using the Flask web framework?使用 Flask web 框架编写通用视图的最佳方法是什么?

Does the @app.route decorator support callable classes? @app.route 装饰器是否支持可调用类? Or am I thinking about this in entirely the wrong fashion?还是我以完全错误的方式思考这个问题?

Any help or advice would be greatly appreciated!任何帮助或建议将不胜感激!

Starting with Python 2.6 you can apply the decorators to classes as well.从 Python 2.6 开始,您也可以将装饰器应用于类。 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.可调用类没有内置模式,因为实现它们的方法太多,但本质上,诀窍是覆盖__call__上的 __call__ 并拥有一个包装装饰器来实例化 class。

I was planning on having a class-based-view extension but so far nobody came up with some good behavior for it:)我正计划有一个基于类的视图扩展,但到目前为止没有人想出一些好的行为:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM