flask中用类的方式构造视图函数

from flask import Flask
from flask.views import MethodView

app = Flask(__name__)


class IndexView(MethodView):
    """
    继承MethodView:它允许把每种HTTP请求方法的处理函数写成一个同名的类方法。
    """

    def get(self):
        return '123'

    def post(self):pass

    def put(self):pass

    def delete(self):pass


# rule:url规则  endpoint:端点名称  name:应该是模块的名称
app.add_url_rule(rule='/', endpoint='index', view_func=IndexView.as_view(name='text6'))

if __name__ == '__main__':
    app.run()
原文地址:https://www.cnblogs.com/yuqiangli0616/p/10345946.html