首页 > 其他 > 详细

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

时间:2020-06-21 15:01:16      阅读:68      评论:0      收藏:0      [点我收藏+]

字符串显示:

@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>

结果:

技术分享图片

 

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

原文:https://www.cnblogs.com/topass123/p/13172430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!