基礎(chǔ)教程: https://www.w3cschool.cn/flask/
官網(wǎng)教程:https://flask.net.cn/tutorial/index.html
學(xué)習(xí)博客:https://blog.csdn.net/weixin_45950544/article/details/104067405
常用擴(kuò)展包
Flask-SQLalchemy:操作數(shù)據(jù)庫(kù)劲适;
Flask-script:插入腳本狞谱;
Flask-migrate:管理遷移數(shù)據(jù)庫(kù)交汤;
Flask-Session:Session存儲(chǔ)方式指定塔淤;
Flask-WTF:表單;
Flask-Mail:郵件炎码;
Flask-Bable:提供國(guó)際化和本地化支持郑叠,翻譯;
Flask-Login:認(rèn)證用戶狀態(tài)产禾;
Flask-OpenID:認(rèn)證排作;
Flask-RESTful:開(kāi)發(fā)REST API的工具;
Flask-Bootstrap:集成前端Twitter Bootstrap框架亚情;
Flask-Moment:本地化日期和時(shí)間妄痪;
Flask-Admin:簡(jiǎn)單而可擴(kuò)展的管理接口的框架
Flask 的擴(kuò)展通常命名為“ Flask-Foo ”或者“ Foo-Flask ” ±慵可以在 PyPI 搜索 標(biāo)記為 Framework :: Flask 擴(kuò)展包衫生。
Flask-SQLalchemy
1.配置:https://flask-sqlalchemy.palletsprojects.com/en/2.x/config/
2.使用命令自動(dòng)生成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/數(shù)據(jù)庫(kù)" --tables 表 --outfile "導(dǎo)出地址" --flask</pre>
學(xué)習(xí)播客:
https://blog.csdn.net/LYLLOAD/article/details/81664322
http://www.reibang.com/p/f7ba338016b8
Flask-RESTful
學(xué)習(xí)博客:
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開(kāi)發(fā)流程</pre>
2
<pre class=" CodeMirror-line " role="presentation"></pre>
3
<pre class=" CodeMirror-line " role="presentation"># 步驟1:創(chuàng)建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:創(chuàng)建藍(lán)圖</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:創(chuàng)建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:注冊(cè)藍(lán)圖</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"># 注冊(cè)路由</pre>
16
<pre class=" CodeMirror-line " role="presentation">api.add_resource(DemoView, '/')</pre>
注意:
Blueprint 并不是一個(gè)完整的應(yīng)用,它不能獨(dú)立于應(yīng)用運(yùn)行土浸,而必須要注冊(cè)到某一個(gè)應(yīng)用中罪针。
注冊(cè)的順序是先將藍(lán)圖綁定到 api 中,這時(shí) api 既有自身的功能黄伊,還有藍(lán)圖的功能泪酱,這是創(chuàng)建 api,藍(lán)圖還需要注冊(cè)到 app 中,最后使用 api 進(jìn)行路由注冊(cè)
注冊(cè)藍(lán)圖和路由的順序不能反西篓,因?yàn)樗{(lán)圖綁定到 api 中了
常用鉤子函數(shù)
學(xué)習(xí)博客:
https://blog.csdn.net/T_I_A_N_/article/details/100086143
藍(lán)圖
學(xué)習(xí)博客:https://zhuanlan.zhihu.com/p/357444025
Flask工廠函數(shù)
學(xué)習(xí)博客:http://www.reibang.com/p/fb6e85c8a3c1
Flask配置
https://zhuanlan.zhihu.com/p/24055329
1.內(nèi)置配置:https://flask.net.cn/config.html https://www.cnblogs.com/songzhixue/p/11171458.html
2.擴(kuò)展庫(kù)配置
a.flask_sqlalchemy:http://www.pythondoc.com/flask-sqlalchemy/config.html
Flask開(kāi)發(fā)流程
<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:自定義工廠函數(shù)</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"> # 初始化各種擴(kuò)展庫(kù)</pre>
15
<pre class=" CodeMirror-line " role="presentation"> db.init_app(app)</pre>
16
17
<pre class=" CodeMirror-line " role="presentation"> # 注冊(cè)藍(lán)圖</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開(kāi)發(fā)接口</pre>
24
<pre class=" CodeMirror-line " role="presentation"># 定義路由:將視圖函數(shù)(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"># 創(chuàng)建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"># 注冊(cè)路由</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:編寫(xiě)view函數(shù)</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>