一、作用
返回给客户端的信息
2、概述
request对象是有服务创建的,response对象需要程序员手动创建
3、创建response对象
导入 from flask import make_response
原型 def make_response(*args): def make_response(info, status, headers):
参数 info 必选参数 返回的数据 status 可选参数 响应状态码 headers 可选参数 子主题
示例
代码语言:javascript复制<span class="hljs-meta">@app.route("/res/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">res</span><span class="hljs-params">()</span>:</span>
response = make_response(<span class="hljs-string">"lucky is a good man"</span>, <span class="hljs-number">200</span>, {<span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"text/plain; charset=utf-8"</span>})
<span class="hljs-keyword">return</span> response
注意
可以直接返回一个字符串,flask会自动包装成response对象
4、响应数据
返回字符串数据
代码语言:javascript复制<span class="hljs-meta">@app.route("/res1/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">res</span><span class="hljs-params">()</span>:</span>
response = make_response(<span class="hljs-string">"lucky is a good man"</span>, <span class="hljs-number">200</span>, {<span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"text/plain; charset=utf-8"</span>})
<span class="hljs-keyword">return</span> response
返回json数据
代码语言:javascript复制<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> json
<span class="hljs-meta">@myApp.route("/res2/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">res3</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">return</span> jsonify({<span class="hljs-string">'name'</span>:<span class="hljs-string">'lucky'</span>, <span class="hljs-string">'age'</span>:<span class="hljs-number">18</span>})
返回页面
代码语言:javascript复制<span class="hljs-meta">@myApp.route("/res3/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">res3</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"index.html"</span>)
5、状态码
作用
告诉客户端对于请求处理结果的状态
使用方式
方式一
代码语言:javascript复制<span class="hljs-meta">@myApp.route("/res4/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">res4</span><span class="hljs-params">()</span>:</span>
response = make_response(<span class="hljs-string">"lucky is a good man"</span>, <span class="hljs-number">999</span>)
<span class="hljs-keyword">return</span> response
方式二
代码语言:javascript复制<span class="hljs-meta">@myApp.route("/res4/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">res4</span><span class="hljs-params">()</span>:</span>
response = make_response(<span class="hljs-string">"lucky is a good man"</span>)
<span class="hljs-keyword">return</span> response,<span class="hljs-number">999</span>
6、重定向 与 反向解析
重定向作用
可以直接通知浏览器转向到另一个地址
导入
from flask import redirect,url_for
redirect:重定向 通过传递路由地址参数 进行重定向跳转
url_for: 反向解析 将当前的视图函数名称反向构造出 路由地址
redirect 重定向使用
代码语言:javascript复制<span class="hljs-comment"># 使用redirect</span>
<span class="hljs-meta">@app.route('/redirect/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_redirect</span><span class="hljs-params">()</span>:</span>
<span class="hljs-comment"># 去首页 无参路由</span>
<span class="hljs-keyword">return</span> redirect(<span class="hljs-string">'/'</span>)
<span class="hljs-comment"># 带一个参数的路由地址的重定向</span>
<span class="hljs-keyword">return</span> redirect(<span class="hljs-string">'/arg/lucky/'</span>)
<span class="hljs-comment"># 带多个参数的路由地址的重定向</span>
<span class="hljs-keyword">return</span> redirect(<span class="hljs-string">'/args/lucky/18/'</span>)
url_for 反向解析使用
代码语言:javascript复制<span class="hljs-comment"># 使用url_for 的视图函数</span>
<span class="hljs-meta">@app.route('/url_for/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_urlfor</span><span class="hljs-params">()</span>:</span>
<span class="hljs-comment"># 构造首页路由</span>
print(url_for(<span class="hljs-string">'index'</span>)) <span class="hljs-comment"># /</span>
<span class="hljs-comment"># 给url_for 一个带参的视图函数 但是没给参数 会报错werkzeug.routing.BuildError: Could not build url for endpoint 'arg'. Did you forget to specify values ['name']?</span>
print(url_for(<span class="hljs-string">'arg'</span>))
<span class="hljs-comment"># 带一个参数 给参数</span>
print(url_for(<span class="hljs-string">'arg'</span>,name=<span class="hljs-string">'lucky'</span>)) <span class="hljs-comment"># /arg/lucky/</span>
<span class="hljs-comment"># 带多个参数的视图函数</span>
<span class="hljs-comment"># werkzeug.routing.BuildError: Could not build url for endpoint 'args'. Did you forget to specify values ['age', 'name']?</span>
print(url_for(<span class="hljs-string">'args'</span>))
<span class="hljs-comment"># 带参数</span>
print(url_for(<span class="hljs-string">'args'</span>,name=<span class="hljs-string">'lucky'</span>,age=<span class="hljs-number">18</span>)) <span class="hljs-comment">#/args/lucky/18/</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'url_for'</span>
redirect和url_for的组合使用
代码语言:javascript复制<span class="hljs-comment"># redirect 和 url_for的组合使用</span>
<span class="hljs-meta">@app.route('/ru/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ru</span><span class="hljs-params">()</span>:</span>
<span class="hljs-comment"># 去首页</span>
<span class="hljs-keyword">return</span> redirect(url_for(<span class="hljs-string">'index'</span>))
<span class="hljs-comment"># 去一个参数的路由地址</span>
<span class="hljs-keyword">return</span> redirect(url_for(<span class="hljs-string">'arg'</span>,name=<span class="hljs-string">'lucky'</span>))
<span class="hljs-comment"># 去多个参数的路由地址</span>
<span class="hljs-keyword">return</span> redirect(url_for(<span class="hljs-string">'args'</span>,name=<span class="hljs-string">'lucky'</span>,age=<span class="hljs-number">18</span>))
7、终止执行
概述
- 如果是视图函数执行过程中出现了错误,可以通过abort函数立即终止视图函数的执行
- 通过abort函数,可以向前端返回一个http标准中存在的状态码,表示出现的错误信息。使用abort函数返回http标准中不存在的状态码是没有任何实际意义的
- 如果触发了abort函数,那么其后面的代码不会被执行 类似于python中的raise
导入
from flask import abort
使用
代码语言:javascript复制<span class="hljs-comment"># 终止</span>
<span class="hljs-meta">@app.route('/abort/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">err</span><span class="hljs-params">()</span>:</span>
<span class="hljs-comment"># 使用abort函数不是说控制权就归你了,它只是向系统抛出了指定的异常</span>
<span class="hljs-comment"># 异常的处理仍然会按照框架中写好方式进行</span>
abort(<span class="hljs-number">404</span>)
<span class="hljs-keyword">return</span> <span class="hljs-string">'abort测试'</span>
<span class="hljs-comment"># 错误页面定制</span>
<span class="hljs-meta">@app.errorhandler(404)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">page_not_found</span><span class="hljs-params">(err)</span>:</span>
<span class="hljs-keyword">return</span> err
<span class="hljs-string">"""
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
"""</span>