当前位置:   article > 正文

【Flask专题】07.响应对象response(PyCharm)_flask框架里面有个make_response函数

flask框架里面有个make_response函数

响应对象response(PyCharm)

视图函数的返回值会自动转换为一个相应对象;
make_response()用于构建response对象;
在视图函数的返回值可以是:

  1. string(系统自动会转换成一个response对象)
  2. make_response()构建的response对象,可添加一些头部信息
  3. dict
  4. 使用jsonify()序列化后的数据,jsonify()将不能当做返回值的数据(如列表。。)序列化,转换为response对象,用于返回
  5. render_template():将独立的模板和视图关联
  6. redirect():重定向

render_template()与redirect()的区别:
render_template():直接渲染模板,浏览器地址栏中的url不会改变,一次请求响应
redirect():重定向页面,用户发送请求(eg. http://127.0.0.1:5000/news),服务器接收到后返回一个response(eg. 包括head[‘location’]=‘/notfound’ code=302),用户接收到重定向后的信息,再次向服务器发送请求(eg. http://127.0.0.1:5000/notfound),服务器再根据路由返回

make_response():构建一个response对象,页面返回该对象

from flask import Flask, make_response
import settings

app = Flask(__name__)
app.config.from_object(settings)


@app.route('/')
def hello_world():
    return 'HELLO world!'  # 响应对象


@app.route('/abc')
def s_abc():
    s = '''<!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>ABC</title>
            </head>
            <body>
                <h1>ABC</h1>
            </body>
            </html>
        '''

    response = make_response(s)
    # 在响应头添加键值对
    response.headers['name'] = 'yyf'
    return response


if __name__ == '__main__':
    app.run()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

返回结果如下:
在这里插入图片描述

在这里插入图片描述

视图函数可以返回字典类型数据

from flask import Flask
import settings

app = Flask(__name__)
app.config.from_object(settings)


@app.route('/')
def hello_world():
    return 'HELLO hello hello hello world!'


@app.route('/abc')
def s_abc():
    dict = {'name': 'yyf', 'age': 20}
    return dict


if __name__ == '__main__':
    app.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

返回结果:
在这里插入图片描述

使用jsonify()将数据转换成response对象,再返回

from flask import Flask, jsonify
import settings

app = Flask(__name__)
app.config.from_object(settings)


@app.route('/')
def hello_world():
    return 'HELLO hello hello hello world!'


@app.route('/abc')
def s_abc():
    list = ['a', 'v', 's', 'e', 'q']
    return jsonify(list)


if __name__ == '__main__':
    app.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

返回结果:
在这里插入图片描述

使用render_template()渲染模板

from flask import Flask, render_template
import settings

app = Flask(__name__)   # template_folder: t.Optional[str] = "templates", 模板默认文件夹为templates
app.config.from_object(settings)


@app.route('/')
def index():
    return render_template('index.html')


if __name__ == '__main__':
    app.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

返回结果:
在这里插入图片描述

实现页面跳转功能

from flask import Flask, render_template, request
import settings

app = Flask(__name__)   # template_folder: t.Optional[str] = "templates", 模板默认文件夹为templates
app.config.from_object(settings)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        pass
    return render_template('login.html')


if __name__ == '__main__':
    app.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
  <a href="/login">登录</a>
  <a href="">注册</a>
  <hr>
  <a href="">新闻</a>
  <a href="">体育</a>
  <a href="">热点</a>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

当点击登录按钮,页面跳转到’/login’页面上

redirect()重定向页面

from flask import Flask, render_template, redirect
import settings

app = Flask(__name__)
app.config.from_object(settings)


@app.route('/')
def index():
    return render_template('index.html')


news = []


@app.route('/news')
def show_news():
    if not news:
        return redirect('/notfound')        # URL:http://127.0.0.1:5000/notfound
        # return render_template('404.html')         # URL:http://127.0.0.1:5000/news


@app.route('/notfound')
def not_found():
    return render_template('404.html')


if __name__ == '__main__':
    app.run()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

当点击新闻时,用户与浏览器发生两次请求响应,如下图:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/414363
推荐阅读
相关标签
  

闽ICP备14008679号