flask之response
import os
from flask import Flask,render_template,redirect,jsonify,send_file
app=Flask(__name__) #开发中开启debug模式,也可以直接在app.run()中设置参数
# app.debug=True
# app.config['DEBUG']=True #(1)flask中的return类似django中的return HttpResponse()
#return直接返回文本内容,在1.1.1版本之后可以返回字符串、字典、元组等,
# The return type must be a string, dict, tuple, Response instance, or WSGI callable
@app.route('/')
def home():
return 'first_flask' # @app.route('/')
# def home():
# return {'key':'first_flask' } #(2)flask中的return render_template() 类似django中的return render()
#return render_teplate()返回静态文件页面
@app.route('/index')
def index():
return render_template('index.html') #(3)flask中的return redirect()类似django中的return redirect()重定向302临时
#return redirect()重定向请求
@app.route('/reback')
def reback():
return redirect('/index') #(4)flask中的jsonify()支持直接发送json数据类型,response-headers中的content-type:applicaiton/json
@app.route('/flask_json')
def flask_json():
return jsonify(['a',2]) #(5)flask中的return send_file()直接可以返回文件
#后端会对send_file返回的文件进行自动识别,类未识别或者浏览器不能解析的就会直接下载
@app.route('/flask_file')
def flask_file():
filepath=os.path.join(os.path.dirname(os.path.abspath(__file__)),'file')
filename='1.png' #Content-Type: image/png
#filename='1.mp3' #Content-Type: audio/mpeg
#filename='1.mp4' #Content-Type: video/mp4
# filename = '1.pdf' #Content-Type: application/pdf
# filename = '1.pptx' #Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
# filename = '1.docx' #Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
# filename='1.zip' #Content-Type: application/x-zip-compressed
# filename='1.rar' #Content-Type: application/octet-stream
file=os.path.join(filepath,filename)
return send_file(file) if __name__ == '__main__':
#flak服务默认端口是5000,可以通过参数指定
# app.run()
app.run(host='192.168.16.14',port=8888,debug=True)

templates模板文件中的页面index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登陆成功,欢迎来到index页面</h1>
<a href="">点击查看数据信息</a>
</body>
</html>
flask之response的更多相关文章
- python web开发-flask中response,cookies,session对象使用详解
Response响应对象: 当一个web请求被服务器处理完后,会返回用户请求的响应,这时候就要用到响应对象,根据响应给用户的形式不同,响应对象有以下几种处理方式 如果返回的是一个合法的响应对象,它会从 ...
- 第二篇 Flask的Response三剑客及两个小儿子
一.Response三剑客 (一)Flask中的HTTPResponse @app.route("/") #app中的route装饰器 def index(): #视图函数 ret ...
- 通过flask中的Response返回json数据
使用flask的过程中,发现有时需要生成一个Response并返回.网上查了查,看了看源码,找到了两种办法: from flask import Response, json Response(jso ...
- flask中的response
1.Response 在flask中你想向前端返回数据,必须是Response的对象,这里和django必须是HttpResponse 对象一样, 主要将返回数据的几种方式 视图函数中return 字 ...
- Flask初学者:视图函数/方法返回值(HTML模板/Response对象)
返回HTML模板:使用“from flask import render_template”,在函数中传入相对于文件夹“templates”HTML模板路径名称字符串即可(默认模板路径),flask会 ...
- Flask中request与response参数
目录 request response request from flask import Flask from flask import request app = Flask(__name__) ...
- flask返回自定义的Response
from json import dumps from flask import Response from flask_api import status from protocol.errors_ ...
- web框架--flask
flask介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...
- Inside Flask - flask.__init__.py 和核心组件
Inside Flask - flask.__init__.py 和核心组件 简单的示例 首先看看一个简单的示例.使用 Flask ,通常是从 flask 模块导入 Flask . request 等 ...
随机推荐
- Python 替换文本中的某些词语
https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python from tempfil ...
- Inno Setup 升级时不再询问用户安装路径
UsePreviousAppDir Description: When this directive is yes, the default, at startup Setup will look i ...
- Decision tree——决策树
基本流程 决策树是通过分次判断样本属性来进行划分样本类别的机器学习模型.每个树的结点选择一个最优属性来进行样本的分流,最终将样本类别划分出来. 决策树的关键就是分流时最优属性$a$的选择.使用所谓信息 ...
- @SessionAttributes 和 @SessionAttribute的区别
@SessionAttributes 和 @SessionAttribute的区别 Spring MVC中有两个长得非常像的注解:@SessionAttributes 和 @SessionAttrib ...
- MySQL根据业务场景归纳常用SQL语句
素材表数据:user[{"id":1,"name":"x"},{"id":2,"name":&quo ...
- 基于LINUX 主机防火墙的端口转发
由于centos7之后将默认防火墙从原来的iptables更改为firewall.本文主要记录基于firewall的端口转发部署. 1.检查防火墙状态 systemctl status fir ...
- windows下git commit使用gvim编辑器
安装gvim 下载安装包:ftp://ftp.vim.org/pub/vim/pc/gvim80-586.exe 安装后将安装路径添加到环境变量Path中 设置编码支持中文 在安装路径下的_vimrc ...
- Oliver Twist
对于济贫院那些绅士们而言,贫民好吃懒做.贪得无厌.他们消耗的食物即是对教区最大的威胁. 绅士们的利益得不到满足时,孤儿们只能被驱之而后快,甚至被"加价出售". 然而,眼泪这种东西根 ...
- Damaged Hard Drive and Reinstall System
0 缘由 我是ACER笔记本,电脑从桌子上重摔,之后几天可以正常使用.可是后来看完视频准备退出的时候,发现所有页面已经卡死了,内存占用已经超过了80%,任务管理器没有反应,不得已按了电源键强制关机. ...
- 2019 ICPC 银川网络赛 H. Fight Against Monsters
It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Eg ...