• flash页面显示处理与数据传递(3)


    字符串显示:

    @app.route('/')
    def hello_world():
        return 'Hello World!'

    json显示:

    不导入jsonify的处理:

    @app.route("/json")
    def json():
        import json
        data = { "a":"b" }
        response = make_response( json.dumps( data ) )
        response.headers["Content-Type"] = "application/json"
        return response

    一般使用导入jsonify的处理形式

    from flash import json,make_response,jsonify
    @app.route("/json")
    def json():
        user={"name":"zhou"}
        response=make_response(jsonify(user))
        return response

    页面渲染【在根级别目录建立文件夹templates/index.html】:

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

    页面渲染间的数据传递实现问题:

    1)传递一般变量

    def index():
        user="xiaoli"
        return render_template("index.html",name=user)
    
    html
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>this is json</p> <p> 用户:{{ name }} </p> </body> </html>

    传递字典,在字典中基于字典或者列表传输数据:

    def index():
        context={}
        context['user'] = { "nickname":"编程浪子","qq":"998945","home_page":"http://www.54php.cn" }
        context['list'] = [1,2,3]
        return render_template("index.html",**context)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <p>this is json</p>
    <p>
    {% if user %}
        {{ user.qq }}
        {% endif %}
    </p>
    <p>
        {% for i in list %}
            {{ i }}
        {% endfor %}
    </p>
    </body>
    </html>

    结果:

  • 相关阅读:
    php echo return exit 区别
    Ubuntu下添加开机启动项的2种方法
    在php中定义常量时,const与define的区别
    yii分页操作
    ubuntu14.04设置静态ip
    Yii IIS8下使用伪静态【Url Rewrite】去掉index.php
    java 第三方库
    IDEA插件
    springboot扫描通用的依赖模块
    idea注册
  • 原文地址:https://www.cnblogs.com/topass123/p/13172430.html
Copyright © 2020-2023  润新知