前言:
Flask框架和Django框架的区别:
- Django框架:
- 大而全,内置的app的很多,第三方app也很多
- Flask框架:
- 小而精,没有过多的内置app,只能完成web框架的基本功能,很多功能都需要借助第三方
拓展:
-
python异步框架:
- fastapi:FastAPI
- sanic
- tornado(目前很少人在使用)
- django:3.x以后版本支持异步
-
同步框架和异步框架的区别
- 同步框架: 一个线程只会处理一个请求
- 异步框架: 一个线程可以处理多个请求
- 异步框架可以显著的提高并发量
一、flask介绍
1、介绍
Flask是一个基于Python开发并且依赖于jinja2模板和Werkzeug WSGI服务的一个微型框架
jinja2:
模板语法,和django的dtl非常像
Werkzeug WSGI:
符合wsgi协议的web服务器,django使用的是wsgiref
2、使用两种协议编写web
使用wsgiref编写web
<span style="color:#393a34"><span style="background-color:white"><code class="language-py"><span style="color:#0000ff">from</span> wsgiref<span style="color:#393a34">.</span>simple_server <span style="color:#0000ff">import</span> make_server<span style="color:#0000ff">def</span> <span style="color:#393a34">mya</span><span style="color:#393a34">(</span>environ<span style="color:#393a34">,</span> start_response<span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#008000"><em># request就是environ包装后的对象</em></span><span style="color:#0000ff">print</span><span style="color:#393a34">(</span>environ<span style="color:#393a34">)</span>start_response<span style="color:#393a34">(</span><span style="color:#a31515">'200 OK'</span><span style="color:#393a34">,</span> <span style="color:#393a34">[</span><span style="color:#393a34">(</span><span style="color:#a31515">'Content-Type'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text/html'</span><span style="color:#393a34">)</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span><span style="color:#008000"><em># 分发路由</em></span><span style="color:#008000"><em># 根据用户访问的路由,打开对应的html文件,读取并返回给用户</em></span><span style="color:#0000ff">if</span> environ<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'PATH_INFO'</span><span style="color:#393a34">)</span> <span style="color:#393a34">==</span> <span style="color:#a31515">'/index'</span><span style="color:#393a34">:</span><span style="color:#0000ff">with</span> open<span style="color:#393a34">(</span><span style="color:#a31515">'index.html'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'rb'</span><span style="color:#393a34">)</span> <span style="color:#0000ff">as</span> f<span style="color:#393a34">:</span>data <span style="color:#393a34">=</span> f<span style="color:#393a34">.</span>read<span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#0000ff">elif</span> environ<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'PATH_INFO'</span><span style="color:#393a34">)</span> <span style="color:#393a34">==</span> <span style="color:#a31515">'/login'</span><span style="color:#393a34">:</span><span style="color:#0000ff">with</span> open<span style="color:#393a34">(</span><span style="color:#a31515">'login.html'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'rb'</span><span style="color:#393a34">)</span> <span style="color:#0000ff">as</span> f<span style="color:#393a34">:</span>data <span style="color:#393a34">=</span> f<span style="color:#393a34">.</span>read<span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#0000ff">else</span><span style="color:#393a34">:</span>data <span style="color:#393a34">=</span> <span style="color:#a31515">b'<h1>Hello Web!</h1>'</span><span style="color:#0000ff">return</span> <span style="color:#393a34">[</span>data<span style="color:#393a34">]</span><span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 第一个参数是服务的IP(不写默认为127.0.0.1),第二个是监听的端口,第三个是编写的web函数</em></span>my_server <span style="color:#393a34">=</span> make_server<span style="color:#393a34">(</span><span style="color:#a31515">'0.0.0.0'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">8008</span><span style="color:#393a34">,</span> mya<span style="color:#393a34">)</span><span style="color:#008000"><em># 启动服务</em></span>my_server<span style="color:#393a34">.</span>serve_forever<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
werkzeug WSGI编写服务:
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># pip 安装werkzeug</em></span>
<span style="color:#008000"><em># 导入</em></span>
<span style="color:#0000ff">from</span> werkzeug<span style="color:#393a34">.</span>wrappers <span style="color:#0000ff">import</span> Request<span style="color:#393a34">,</span> Response<span style="color:#393a34">@Request<span style="color:#393a34">.</span>application</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">my_server</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#0000ff">print</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span><span style="color:#0000ff">return</span> Response<span style="color:#393a34">(</span><span style="color:#a31515">'Hello Web!'</span><span style="color:#393a34">)</span><span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 导入启动服务的模块</em></span><span style="color:#0000ff">from</span> werkzeug<span style="color:#393a34">.</span>serving <span style="color:#0000ff">import</span> run_simplerun_simple<span style="color:#393a34">(</span><span style="color:#a31515">'127.0.0.1'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">4000</span><span style="color:#393a34">,</span> my_server<span style="color:#393a34">)</span>
</code></span></span>
二、flask快速使用
安装:
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 安装flask会一并安装其依赖:jinja2、Werkzeug、MarkupSafe</em></span>pip install flask <span style="color:#008000"><em># 版本问题:</em></span><span style="color:#393a34">-</span><span style="color:#36acaa">1</span><span style="color:#393a34">.</span>x 没有本质区别<span style="color:#393a34">-</span><span style="color:#36acaa">2</span><span style="color:#393a34">.</span>x 没有本质区别,源码上动了,用起来一样
</code></span></span>
1、快速使用:
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 导入模块</em></span>
<span style="color:#0000ff">from</span> flask <span style="color:#0000ff">import</span> Flask<span style="color:#008000"><em># 实例化对象,参数内是服务的名字,填入任意都可以</em></span>app <span style="color:#393a34">=</span> Flask<span style="color:#393a34">(</span>__name__<span style="color:#393a34">)</span><span style="color:#008000"><em># 编写函数、注册路由(装饰器方法注册)</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#0000ff">return</span> <span style="color:#a31515">'hello web!'</span><span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/home'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">home</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#0000ff">return</span> <span style="color:#a31515">'hello home!'</span><span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span><span style="color:#008000"><em># app.run('127.0.0.1', 5000)</em></span><span style="color:#008000"><em># 默认监听本地127.0.0.1的5000端口</em></span>app<span style="color:#393a34">.</span>run<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
2、使用flask编写登录小案例
2.1 login.html
<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>form</span> <span style="color:#ff0000">method</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>post<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>用户名:<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>text<span style="color:#393a34">"</span></span> <span style="color:#ff0000">name</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>username<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>密码:<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>password<span style="color:#393a34">"</span></span> <span style="color:#ff0000">name</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>password<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>submit<span style="color:#393a34">"</span></span> <span style="color:#ff0000">value</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>登录<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span> {{error}}
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>form</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>
2.2 home.html
<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>h1</span><span style="color:#393a34">></span></span>用户列表<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>h1</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>table</span><span style="color:#393a34">></span></span>{% for k,v in user_dict.items() %}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>tr</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{k}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v.name}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v['name']}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v.get('name')}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>a</span> <span style="color:#ff0000">href</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>/detail/{{k}}<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>查看详细<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>a</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>tr</span><span style="color:#393a34">></span></span>{% endfor %}
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>table</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>
2.3 detail.html
<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>名字是:{{user.name}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>年龄是:{{user['age']}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>性别是:{{user.get('gender')}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>{{user.text}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>
2.4 py文件
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#0000ff">from</span> flask <span style="color:#0000ff">import</span> Flask<span style="color:#393a34">,</span> request<span style="color:#393a34">,</span> render_template<span style="color:#393a34">,</span> session<span style="color:#393a34">,</span> redirectapp <span style="color:#393a34">=</span> Flask<span style="color:#393a34">(</span>__name__<span style="color:#393a34">)</span><span style="color:#008000"><em># 使用session需要指定key</em></span>
app<span style="color:#393a34">.</span>secret_key <span style="color:#393a34">=</span> <span style="color:#a31515">'abc123'</span>USERS <span style="color:#393a34">=</span> <span style="color:#393a34">{</span><span style="color:#36acaa">1</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'张三'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">18</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'男'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"道路千万条"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span><span style="color:#36acaa">2</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'李四'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">28</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'男'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"安全第一条"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span><span style="color:#36acaa">3</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'王五'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">18</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'女'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"行车不规范"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
<span style="color:#393a34">}</span><span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">,</span> methods<span style="color:#393a34">=</span><span style="color:#393a34">[</span><span style="color:#a31515">'GET'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'POST'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 判断路由的方式</em></span><span style="color:#0000ff">if</span> request<span style="color:#393a34">.</span>method <span style="color:#393a34">==</span> <span style="color:#a31515">'GET'</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 返回登陆页面给用户</em></span><span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Login.html'</span><span style="color:#393a34">)</span><span style="color:#008000"><em># post请求判断用户名密码</em></span><span style="color:#0000ff">if</span> request<span style="color:#393a34">.</span>method <span style="color:#393a34">==</span> <span style="color:#a31515">'POST'</span><span style="color:#393a34">:</span>username <span style="color:#393a34">=</span> request<span style="color:#393a34">.</span>form<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'username'</span><span style="color:#393a34">)</span>password <span style="color:#393a34">=</span> request<span style="color:#393a34">.</span>form<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'password'</span><span style="color:#393a34">)</span><span style="color:#008000"><em># 校验用户名或密码</em></span><span style="color:#0000ff">if</span> username <span style="color:#393a34">==</span> <span style="color:#a31515">'kangkang'</span> <span style="color:#0000ff">and</span> password <span style="color:#393a34">==</span> <span style="color:#a31515">'123'</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 校验成功,保存session(导入、全局使用)</em></span>session<span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">]</span> <span style="color:#393a34">=</span> username<span style="color:#008000"><em># 重定向到home页面(导入redirect)</em></span><span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span><span style="color:#0000ff">else</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 用户名或密码错误</em></span><span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Login.html'</span><span style="color:#393a34">,</span> error<span style="color:#393a34">=</span><span style="color:#a31515">'用户名或密码错误'</span><span style="color:#393a34">)</span><span style="color:#008000"><em># 编写首页</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">home</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 先校验用户是否登录</em></span><span style="color:#0000ff">if</span> session<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 校验登录通过,展示首页</em></span><span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Home.html'</span><span style="color:#393a34">,</span> user_dict<span style="color:#393a34">=</span>USERS<span style="color:#393a34">)</span><span style="color:#0000ff">else</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 没有登陆跳转到登陆页面</em></span><span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span><span style="color:#008000"><em># 编写用户详情页</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/detail/<int:pk>'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">detail</span><span style="color:#393a34">(</span>pk<span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 先校验用户是否登录</em></span><span style="color:#0000ff">if</span> session<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 校验登录通过,展示详情页面</em></span>user_detail <span style="color:#393a34">=</span> USERS<span style="color:#393a34">[</span>pk<span style="color:#393a34">]</span><span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Detail.html'</span><span style="color:#393a34">,</span> user<span style="color:#393a34">=</span>user_detail<span style="color:#393a34">)</span><span style="color:#0000ff">else</span><span style="color:#393a34">:</span><span style="color:#008000"><em># 没有登陆跳转到登陆页面</em></span><span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span><span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>app<span style="color:#393a34">.</span>run<span style="color:#393a34">(</span><span style="color:#393a34">)</span></code></span></span>
三、flask配置文件
1、配置文件的几种方式
flask不同于django可以在settings文件编写配置,flask配置文件的方式有多种,相较于django更加灵活
方式一:直接编写
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 在编写app的我呢见中直接编写配置(用于测试)</em></span>app<span style="color:#393a34">.</span>debug<span style="color:#393a34">=</span><span style="color:#36acaa">True</span> <span style="color:#008000"><em># 调试模式,提示信息更详细,修改代码不需要重启,自动重启</em></span>app<span style="color:#393a34">.</span>secret_key<span style="color:#393a34">=</span><span style="color:#a31515">'dasdfasdfasd'</span> <span style="color:#008000"><em># 秘钥,只能 放debug和secret_key</em></span>
</code></span></span>
方式二:使用app.config
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 直接使用flask实例化的对象点出config的方式添加</em></span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">[</span><span style="color:#a31515">'DEBUG'</span><span style="color:#393a34">]</span><span style="color:#393a34">=</span><span style="color:#36acaa">True</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">[</span><span style="color:#a31515">'SECRET_KEY'</span><span style="color:#393a34">]</span><span style="color:#393a34">=</span><span style="color:#a31515">'sdfasdfasd'</span><span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>
方式三:使用py文件,然后载入
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 将配置编写在py文件中,然后使用方法导入(不常用)</em></span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_pyfile<span style="color:#393a34">(</span><span style="color:#a31515">"settings.py"</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 变量必须大写</em></span><span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>
方式四:使用类导入
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 同样是创建py文件,区别是写在类中,可以上线时候可以指定使用哪套</em></span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_object<span style="color:#393a34">(</span><span style="color:#a31515">'settings.DevelopmentConfig'</span><span style="color:#393a34">)</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_object<span style="color:#393a34">(</span><span style="color:#a31515">'settings.ProductionConfig'</span><span style="color:#393a34">)</span><span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>
方式五:其他方式
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 1、通过环境变量导入</em></span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_envvar<span style="color:#393a34">(</span><span style="color:#a31515">"环境变量名称"</span><span style="color:#393a34">)</span><span style="color:#008000"><em># 2、通过json文件载入</em></span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_json<span style="color:#393a34">(</span><span style="color:#a31515">"json文件名称"</span><span style="color:#393a34">)</span><span style="color:#008000"><em># JSON文件名称,必须是json格式,因为内部会执行json.loads</em></span><span style="color:#008000"><em># 3、字典格式、配置中心</em></span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_mapping<span style="color:#393a34">(</span><span style="color:#393a34">{</span><span style="color:#a31515">'DEBUG'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">True</span><span style="color:#393a34">}</span><span style="color:#393a34">)</span>
</code></span></span>
2、常用的配置字段
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#393a34">-</span>DEBUG <span style="color:#008000"><em># debug模式</em></span>
<span style="color:#393a34">-</span>SECRET_KEY <span style="color:#008000"><em># session的key值 (密钥)</em></span>
<span style="color:#393a34">-</span>SESSION_COOKIE_NAME <span style="color:#008000"><em># 用户浏览器上cokie会变成设置的名字</em></span>
<span style="color:#393a34">-</span>PERMANENT_SESSION_LIFETIME <span style="color:#008000"><em># session过期时间</em></span><span style="color:#008000"><em># 内置的配置字段,其他可以写自己的,比如 redis的连接地址,mysql的连接地址</em></span>
</code></span></span>
四、路由系统
1、路由的本质
在django中,路由写在urls.py文件下的path列表中
flask是基于装饰器的,大部分都是使用装饰器来做,少量的可以抽取到urls.py中
路由装饰器源码分析:
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 咱们这样写</em></span><span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span><span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span><span style="color:#0000ff">pass</span><span style="color:#008000"><em>#本质是---》index=app.route('/login')(index)</em></span><span style="color:#008000"><em># app.route('/login')的执行结果 decorator 函数</em></span><span style="color:#393a34">-</span>rule是路径<span style="color:#393a34">-</span>其他参数都给了options<span style="color:#008000"><em># 然后 decorator(index)--->在执行</em></span><span style="color:#008000"><em># f是index</em></span>endpoint <span style="color:#393a34">=</span> options<span style="color:#393a34">.</span>pop<span style="color:#393a34">(</span><span style="color:#a31515">"endpoint"</span><span style="color:#393a34">,</span> <span style="color:#36acaa">None</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 目前没有endpoint,是None</em></span><span style="color:#008000"><em># 核心,本质--》self就是实例化得到的app对象,flask对象</em></span><span style="color:#008000"><em># app对象中有个方法add_url_rule,这是在添加路由</em></span><span style="color:#008000"><em># 不使用装饰器,自己注册路由</em></span>self<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span>rule<span style="color:#393a34">,</span> endpoint<span style="color:#393a34">,</span> f<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">)</span><span style="color:#0000ff">return</span> f<span style="color:#0000ff">def</span> <span style="color:#393a34">route</span><span style="color:#393a34">(</span>self<span style="color:#393a34">,</span> rule<span style="color:#393a34">:</span> str<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">:</span> t<span style="color:#393a34">.</span>Any<span style="color:#393a34">)</span> <span style="color:#393a34">-</span><span style="color:#393a34">></span> t<span style="color:#393a34">.</span>Callable<span style="color:#393a34">[</span><span style="color:#393a34">[</span>T_route<span style="color:#393a34">]</span><span style="color:#393a34">,</span> T_route<span style="color:#393a34">]</span><span style="color:#393a34">:</span><span style="color:#0000ff">def</span> <span style="color:#393a34">decorator</span><span style="color:#393a34">(</span>f<span style="color:#393a34">:</span> T_route<span style="color:#393a34">)</span> <span style="color:#393a34">-</span><span style="color:#393a34">></span> T_route<span style="color:#393a34">:</span>endpoint <span style="color:#393a34">=</span> options<span style="color:#393a34">.</span>pop<span style="color:#393a34">(</span><span style="color:#a31515">"endpoint"</span><span style="color:#393a34">,</span> <span style="color:#36acaa">None</span><span style="color:#393a34">)</span>self<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span>rule<span style="color:#393a34">,</span> endpoint<span style="color:#393a34">,</span> f<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">)</span><span style="color:#0000ff">return</span> f<span style="color:#0000ff">return</span> decorator<span style="color:#008000"><em># 可以不使用装饰器的方式,注册路由</em></span>app<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">,</span> endpoint<span style="color:#393a34">=</span><span style="color:#36acaa">None</span><span style="color:#393a34">,</span> view_func<span style="color:#393a34">=</span>home<span style="color:#393a34">,</span> methods<span style="color:#393a34">=</span><span style="color:#393a34">[</span><span style="color:#a31515">'GET'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span><span style="color:#008000"><em># flask路由的本质是app对象的add_url_rule完成路由的注册</em></span>
</code></span></span>
2、add_url_rule参数
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># rule URL规则</em></span>
<span style="color:#008000"><em># view_func 视图函数名称</em></span>
<span style="color:#008000"><em># defaults = None 默认值, 当URL中无参数,函数需要参数时,使用defaults = {'k': 'v'}为函数提供参数</em></span>
<span style="color:#008000"><em># endpoint = None, 路径的别名,名称,用于反向解析URL,即: url_for('名称')</em></span>
<span style="color:#008000"><em># methods = None, 允许的请求方式,如:["GET", "POST"]</em></span><span style="color:#008000"><em>#对URL最后的 / 符号是否严格要求</em></span>
strict_slashes <span style="color:#393a34">=</span> <span style="color:#36acaa">None</span><span style="color:#a31515">'''@app.route('/index', strict_slashes=False)#访问http://www.xx.com/index/ 或http://www.xx.com/index均可@app.route('/index', strict_slashes=True)#仅访问http://www.xx.com/index'''</span><span style="color:#008000"><em>#重定向到指定地址</em></span>
redirect_to <span style="color:#393a34">=</span> <span style="color:#36acaa">None</span><span style="color:#393a34">,</span> <span style="color:#a31515">'''@app.route('/index/<int:nid>', redirect_to='/home/<nid>')'''</span><span style="color:#008000"><em># 需要记住的</em></span><span style="color:#008000"><em># rule </em></span><span style="color:#008000"><em># view_func </em></span><span style="color:#008000"><em># defaults</em></span><span style="color:#008000"><em># endpoint</em></span><span style="color:#008000"><em># methods</em></span>
</code></span></span>
3、转换器
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"> <span style="color:#a31515">'default'</span><span style="color:#393a34">:</span> UnicodeConverter<span style="color:#393a34">,</span><span style="color:#a31515">'string'</span><span style="color:#393a34">:</span> UnicodeConverter<span style="color:#393a34">,</span><span style="color:#a31515">'any'</span><span style="color:#393a34">:</span> AnyConverter<span style="color:#393a34">,</span><span style="color:#a31515">'path'</span><span style="color:#393a34">:</span> PathConverter<span style="color:#393a34">,</span><span style="color:#a31515">'int'</span><span style="color:#393a34">:</span> IntegerConverter<span style="color:#393a34">,</span><span style="color:#a31515">'float'</span><span style="color:#393a34">:</span> FloatConverter<span style="color:#393a34">,</span><span style="color:#a31515">'uuid'</span><span style="color:#393a34">:</span> UUIDConverter<span style="color:#393a34">,</span><span style="color:#008000"><em># 了解:让路由支持正则(忽略掉)</em></span></code></span></span>