微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

结果:

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐