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的更多相关文章

  1. python web开发-flask中response,cookies,session对象使用详解

    Response响应对象: 当一个web请求被服务器处理完后,会返回用户请求的响应,这时候就要用到响应对象,根据响应给用户的形式不同,响应对象有以下几种处理方式 如果返回的是一个合法的响应对象,它会从 ...

  2. 第二篇 Flask的Response三剑客及两个小儿子

    一.Response三剑客 (一)Flask中的HTTPResponse @app.route("/") #app中的route装饰器 def index(): #视图函数 ret ...

  3. 通过flask中的Response返回json数据

    使用flask的过程中,发现有时需要生成一个Response并返回.网上查了查,看了看源码,找到了两种办法: from flask import Response, json Response(jso ...

  4. flask中的response

    1.Response 在flask中你想向前端返回数据,必须是Response的对象,这里和django必须是HttpResponse 对象一样, 主要将返回数据的几种方式 视图函数中return 字 ...

  5. Flask初学者:视图函数/方法返回值(HTML模板/Response对象)

    返回HTML模板:使用“from flask import render_template”,在函数中传入相对于文件夹“templates”HTML模板路径名称字符串即可(默认模板路径),flask会 ...

  6. Flask中request与response参数

    目录 request response request from flask import Flask from flask import request app = Flask(__name__) ...

  7. flask返回自定义的Response

    from json import dumps from flask import Response from flask_api import status from protocol.errors_ ...

  8. web框架--flask

    flask介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...

  9. Inside Flask - flask.__init__.py 和核心组件

    Inside Flask - flask.__init__.py 和核心组件 简单的示例 首先看看一个简单的示例.使用 Flask ,通常是从 flask 模块导入 Flask . request 等 ...

随机推荐

  1. php正则匹配到字符串里面的a标签

    $cont = preg_replace('/<a href=\"(.*?)\".*?>(.*?)<\/a>/i','',$cont);

  2. java list随机截取(洗牌)

    public void solution(){ List<Integer> givenList = Arrays.asList(1, 2, 3,4,5,6); Collections.sh ...

  3. [Batch脚本] if else 的格式

    必须写成一行 ) else (,否则报错. if %abc%=="yes" ( ... ) else ( ... )

  4. 实用的linux 命令(上)

    今天介绍几个我常用的Linux 命令,每个命令这里只介绍其常用参数. 对于每个Linux 命令都可以使用man + 命令名称,查看其完整使用方法. 0,man man 命令是一个非常有用的命令,当你不 ...

  5. vue项目中使用bpmn-为节点添加颜色

    内容概述 本系列 “vue项目中使用bpmn-xxxx” 分为五篇,均为自己使用过程中用到的实例,手工原创,目前属于陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意 ...

  6. Spring Boot JPA中关联表的使用

    文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...

  7. 计算2的n次幂htm代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. javascript SDK开发之webpack打包支持对象展开运算符...

    场景:使用了对象的展开运算符,webpack打包报错,错误如下:Parsing error: Unexpected token - 1.安装依赖 npm install babel-preset-st ...

  9. js中的this指针的用法

    首先看下面代码: function funcA() { this.name = "hello"; console.log(this.name); this.show = funct ...

  10. 几年前的今天,Google发了这几篇“大”新闻

    免责声明: 因阅读本文所导致的任何时间或经济上的损失,皆由您自行承担,本小编概不负责. 估计今天我的朋友圈会被"震惊!"刷屏,来看看 Google 做过哪些令人"震惊&q ...