1、最简单的开始
1 from flask import Flask 2 3 app = Flask(__name__) 4 5 6 @app.route(‘/‘) 7 def hello_world(): 8 return ‘Hello World!‘ 9 10 11 if __name__ == ‘__main__‘: 12 app.run()
2、Jinja2模板、自定义错误页面、静态文件
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 5 @app.route(‘/‘) 6 def index(): 7 return render_template(‘index.html‘) 8 9 @app.route(‘/user/<name>‘) 10 def user(name): 11 return render_template(‘user.html‘,name=name) 12 13 @app.route(‘/hello_world‘) 14 def hello_world(): 15 return ‘Hello World!‘ 16 17 @app.errorhandler(404) 18 def page_not_found(e): 19 return render_template(‘404.html‘),404 20 21 @app.errorhandler(500) 22 def errorhandler(e): 23 return render_template(‘500.html‘),500 24 25 if __name__ == ‘__main__‘: 26 app.run()
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 {% block head %} 6 <title>----{% block title %}{% endblock %}----</title> 7 {% endblock %} 8 </head> 9 <body> 10 <p>Flask的Base页面</p> 11 {% block body %} 12 {% endblock %} 13 </body> 14 </html>
1 {% extends ‘base.html‘ %} 2 <!--head在基模板中不是空的,使用super()获取原本内容--> 3 {% block head %}{{ super() }}{% endblock %} 4 {% block title %}首页{% endblock %} 5 {% block body %}首页的body{% endblock %}
1 {% extends ‘base.html‘ %} 2 {% block head %}{{ super() }}{% endblock %} 3 {% block title %}用户--{{ name }}{% endblock %} 4 {% block body %}{% endblock %}
1 {% extends ‘base.html‘ %} 2 {% block head %}{{ super() }}{% endblock %} 3 {% block title %}404{% endblock %} 4 {% block body %} 5 <image src="{{ url_for(‘static‘,filename=‘images/404.jpg‘) }}"></image> 6 {% endblock %}
1 {% extends ‘base.html‘ %} 2 {% block head %}{{ super() }}{% endblock %} 3 {% block title %}500{% endblock %} 4 {% block body %}<h1>500</h1>{% endblock %}
3、Web表单 | Flask-WTF
原文:https://www.cnblogs.com/cx59244405/p/9745114.html