基础教程: https://www.w3cschool.cn/flask/
官网教程:https://flask.net.cn/tutorial/index.html
学习博客:https://blog.csdn.net/weixin_45950544/article/details/104067405
常用扩展包
Flask-SQLalchemy:操作数据库;
Flask-script:插入脚本;
Flask-migrate:管理迁移数据库;
Flask-Session:Session存储方式指定;
Flask-WTF:表单;
Flask-Mail:邮件;
Flask-Bable:提供国际化和本地化支持,翻译;
Flask-Login:认证用户状态;
Flask-OpenID:认证;
Flask-RESTful:开发REST API的工具;
Flask-Bootstrap:集成前端Twitter Bootstrap框架;
Flask-Moment:本地化日期和时间;
Flask-Admin:简单而可扩展的管理接口的框架
Flask 的扩展通常命名为“ Flask-Foo ”或者“ Foo-Flask ” 。可以在 PyPI 搜索 标记为 Framework :: Flask 扩展包。
Flask-SQLalchemy
1.配置:https://flask-sqlalchemy.palletsprojects.com/en/2.x/config/
2.使用命令自动生成model
<input autocomplete="off" type="search" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_9_list" aria-autocomplete="list" aria-controls="rc_select_9_list" aria-activedescendant="rc_select_9_list_0" value="" id="rc_select_9">Shell
收 起
<textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; min-height: 1em; outline: none;"></textarea>
<pre class="CodeMirror-line-like">xxxxxxxxxx</pre>
1
<pre class=" CodeMirror-line " role="presentation">pip install flask-sqlacodegen</pre>
2
<pre class=" CodeMirror-line " role="presentation">flask-sqlacodegen "mysql+pymysql://用户名:密码@host:port/数据库" --tables 表 --outfile "导出地址" --flask</pre>
学习播客:
https://blog.csdn.net/LYLLOAD/article/details/81664322
//www.greatytc.com/p/f7ba338016b8
Flask-RESTful
学习博客:
https://www.cnblogs.com/donghaoblogs/p/10389696.html
http://www.pythondoc.com/Flask-RESTful/quickstart.html
<input autocomplete="off" type="search" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_10_list" aria-autocomplete="list" aria-controls="rc_select_10_list" aria-activedescendant="rc_select_10_list_0" value="" id="rc_select_10">Python
收 起
<textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; min-height: 1em; outline: none;"></textarea>
<pre class="CodeMirror-line-like">xxxxxxxxxx</pre>
16
1
<pre class=" CodeMirror-line " role="presentation"># flask-restful + blueprint开发流程</pre>
2
<pre class=" CodeMirror-line " role="presentation"></pre>
3
<pre class=" CodeMirror-line " role="presentation"># 步骤1:创建app</pre>
4
<pre class=" CodeMirror-line " role="presentation">app = Flask(name)</pre>
5
<pre class=" CodeMirror-line " role="presentation"></pre>
6
<pre class=" CodeMirror-line " role="presentation"># 步骤2:创建蓝图</pre>
7
<pre class=" CodeMirror-line " role="presentation">blueprint = Blueprint('api', name)</pre>
8
<pre class=" CodeMirror-line " role="presentation"></pre>
9
<pre class=" CodeMirror-line " role="presentation"># 步骤3:创建api</pre>
10
<pre class=" CodeMirror-line " role="presentation">api = Api(blueprint)</pre>
11
<pre class=" CodeMirror-line " role="presentation"></pre>
12
<pre class=" CodeMirror-line " role="presentation"># 步骤4:注册蓝图</pre>
13
<pre class=" CodeMirror-line " role="presentation">app.register_blueprint(blueprint)</pre>
14
<pre class=" CodeMirror-line " role="presentation"></pre>
15
<pre class=" CodeMirror-line " role="presentation"># 注册路由</pre>
16
<pre class=" CodeMirror-line " role="presentation">api.add_resource(DemoView, '/')</pre>
注意:
Blueprint 并不是一个完整的应用,它不能独立于应用运行,而必须要注册到某一个应用中。
注册的顺序是先将蓝图绑定到 api 中,这时 api 既有自身的功能,还有蓝图的功能,这是创建 api,蓝图还需要注册到 app 中,最后使用 api 进行路由注册
注册蓝图和路由的顺序不能反,因为蓝图绑定到 api 中了
常用钩子函数
学习博客:
https://blog.csdn.net/T_I_A_N_/article/details/100086143
蓝图
学习博客:https://zhuanlan.zhihu.com/p/357444025
Flask工厂函数
学习博客://www.greatytc.com/p/fb6e85c8a3c1
Flask配置
https://zhuanlan.zhihu.com/p/24055329
1.内置配置:https://flask.net.cn/config.html https://www.cnblogs.com/songzhixue/p/11171458.html
2.扩展库配置
a.flask_sqlalchemy:http://www.pythondoc.com/flask-sqlalchemy/config.html
Flask开发流程
<input autocomplete="off" type="search" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_11_list" aria-autocomplete="list" aria-controls="rc_select_11_list" aria-activedescendant="rc_select_11_list_0" value="" id="rc_select_11">Python
收 起
<textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; min-height: 1em; outline: none;"></textarea>
<pre class="CodeMirror-line-like">x</pre>
1
<pre class=" CodeMirror-line " role="presentation">from flask import Flask</pre>
2
<pre class=" CodeMirror-line " role="presentation">from config import Config</pre>
3
<pre class=" CodeMirror-line " role="presentation"></pre>
4
<pre class=" CodeMirror-line " role="presentation">from flask_sqlalchemy import SQLAlchemy</pre>
5
<pre class=" CodeMirror-line " role="presentation"></pre>
6
<pre class=" CodeMirror-line " role="presentation">db = SQLAlchemy()</pre>
7
<pre class=" CodeMirror-line " role="presentation"></pre>
8
<pre class=" CodeMirror-line " role="presentation"># 步骤1:自定义工厂函数</pre>
9
<pre class=" CodeMirror-line " role="presentation">def create_app()</pre>
10
<pre class=" CodeMirror-line " role="presentation"> app = Flask(name)</pre>
11
<pre class=" CodeMirror-line " role="presentation"> # 加载配置文件</pre>
12
<pre class=" CodeMirror-line " role="presentation"> app.config.from_object(Config)</pre>
13
14
<pre class=" CodeMirror-line " role="presentation"> # 初始化各种扩展库</pre>
15
<pre class=" CodeMirror-line " role="presentation"> db.init_app(app)</pre>
16
17
<pre class=" CodeMirror-line " role="presentation"> # 注册蓝图</pre>
18
<pre class=" CodeMirror-line " role="presentation"> from app.routes import main_routes</pre>
19
<pre class=" CodeMirror-line " role="presentation"> app.register_blueprint(main_routes)</pre>
20
<pre class=" CodeMirror-line " role="presentation"> return app</pre>
21
<pre class=" CodeMirror-line " role="presentation"># -----------------------------------------</pre>
22
<pre class=" CodeMirror-line " role="presentation"></pre>
23
<pre class=" CodeMirror-line " role="presentation"># 步骤2:使用flask-restful + blueprint开发接口</pre>
24
<pre class=" CodeMirror-line " role="presentation"># 定义路由:将视图函数(view)绑定到url</pre>
25
<pre class=" CodeMirror-line " role="presentation">from flask import Blueprint</pre>
26
<pre class=" CodeMirror-line " role="presentation">from flask_restful import Api</pre>
27
<pre class=" CodeMirror-line " role="presentation">from . import view</pre>
28
<pre class=" CodeMirror-line " role="presentation"></pre>
29
<pre class=" CodeMirror-line " role="presentation">bp = Blueprint('hello_world', name, url_prefix='/hello_world')</pre>
30
<pre class=" CodeMirror-line " role="presentation"></pre>
31
<pre class=" CodeMirror-line " role="presentation"># 创建api</pre>
32
<pre class=" CodeMirror-line " role="presentation">api = Api(bp)</pre>
33
<pre class=" CodeMirror-line " role="presentation"></pre>
34
<pre class=" CodeMirror-line " role="presentation"># 注册路由</pre>
35
<pre class=" CodeMirror-line " role="presentation">api.add_resource(DemoView, '/')</pre>
36
<pre class=" CodeMirror-line " role="presentation"></pre>
37
<pre class=" CodeMirror-line " role="presentation"># --------------------------------------</pre>
38
<pre class=" CodeMirror-line " role="presentation"># 步骤3:编写view函数</pre>
39
<pre class=" CodeMirror-line " role="presentation">class DemoView(Resource):</pre>
40
<pre class=" CodeMirror-line " role="presentation"> def get(self):</pre>
41
<pre class=" CodeMirror-line " role="presentation"> return 'hello world'</pre>