立即学习:https://edu.csdn.net/course/play/27093/354195?utm_source=blogtoedu
flask标准的类视图
1、必须继承flask.veiws.View
2、必须实现dispatch_request
3、必须通过app.add_url_rule()来做url映射
如
from flask import Flask, render_template, views
app = Flask(__name__)
class glo(views.View):
def __init__(self):
self.text = {
'text': '大兵是讲师'
}
class index(glo):
def dispatch_request(self):
return render_template('index.html', **self.text)
class dabing(glo):
def dispatch_request(self):
return render_template('dabing.html', **self.text)
app.add_url_rule(rule='/', endpoint='index', view_func=index.as_view('index'))
app.add_url_rule(rule='/dabing/', endpoint='dabing', view_func=dabing.as_view('dabing'))
if __name__ == '__main__':
app.run()
本文详细介绍使用Flask框架的标准类视图实现方法,包括必须继承的View基类,实现dispatch_request方法,以及如何通过app.add_url_rule进行URL映射。通过具体代码示例展示如何创建多个类视图并映射到不同URL。
232

被折叠的 条评论
为什么被折叠?



