zoukankan      html  css  js  c++  java
  • Flask 的 5 种返回值

    一、字符串

    # 返回值是字符串
    @app.route('/one')
    def one():
        return "This is a string!"
    

    在这里插入图片描述

    二、重定向

    # 引入重定向函数
    from flask import redirect
    # 返回值是路由的重定向,和第一个的页面相同
    @app.route('/two')
    def two():
        return redirect("/one")
    

    三、html界面渲染

    # 引入渲染函数
    from flask import render_template
    # 返回值是html界面渲染
    @app.route('/three')
    def three():
        return render_template("hello.html", name="xkj")
    

    注意:hello.html文件是在templets文件下直接创建的!
    在这里插入图片描述

    四、返回文件

    # 引入发送文件的函数
    from flask import send_file
    # 打开并返回文件内容
    @app.route('/four')
    def four():
        return send_file("4.jpeg")
    

    在这里插入图片描述

    五、返回json

    #引入返回json的函数
    from flask import jsonify
    #返回json
    @app.route('/five')
    def five():
        obj = {
            "status": 1,
            "content": {
                "from": "python"
            }
        }
        return jsonify(obj)
    

    在这里插入图片描述

    本文首发于python黑洞网,博客园同步更新

  • 相关阅读:
    winform 计算器
    ajax无刷新上传图片
    Dapper的基本使用
    补充1
    Ajax2
    Ajax1
    jQuery2
    Select查询语句2
    jQuery1
    分页与组合查询
  • 原文地址:https://www.cnblogs.com/pythonzhilian/p/13814150.html
Copyright © 2011-2022 走看看