日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python笔记-Flask返回字符串、Json、模板数据

發布時間:2025/3/15 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python笔记-Flask返回字符串、Json、模板数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里主要是回數據給前端,其中Json,和模板最為常見

程序運行截圖如下:

字符串:

Json:

模板:

程序結構如下:

源碼如下:

application.py

from flask import Flask from controller import index_pageapp = Flask(__name__)app.register_blueprint(index_page, url_prefix = "/it1995")if __name__ == "__main__":app.run(host = "0.0.0.0", debug=True)

controller.py

from flask import Flask, Blueprint, request, make_response, jsonify, render_templateindex_page = Blueprint("index_page", __name__)@index_page.route("/text") def text():return "text/html"@index_page.route("/text_same") def text_same():response = make_response("text/html", 200)return response@index_page.route("/json") def json():import jsondata = {"a" : "b"}response = make_response(json.dumps(data))response.headers["Content-Type"] = "application/json"return response@index_page.route("/json_same") def json_same():data = {"a" : "b"}response = make_response(jsonify(data))return response@index_page.route("/template") def template():name = "Hello World"context = {"name" : name}context['user'] = {"nickname" : "IT1995", "qq" : "570176391", "url" : "www.it1995.cn"}context['num_list'] = [1, 2, 3, 4, 5]return render_template("index.html", **context)@index_page.route("/index3") def index3page():return render_template("index3.html")@index_page.route("/index4") def index4page():return render_template("index4.html")

layout.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>統一模版</title> </head> <body> {%block content%} {%endblock%} </body> </html>

index.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> Hello <p>{% if user %}{{user.nickname}} 聯系QQ: {{user.qq}} 主頁: {{user.url}}{% endif %} </p> <p>{% for tmp_num in num_list %}{{tmp_num}}{% endfor %} </p> </body> </html>

index3.html

{% extends "common/layout.html" %} {% block content %} How are you {% endblock %}

index4.html

{% extends "common/layout.html" %} {% block content %} How old are you {% endblock %}

?

總結

以上是生活随笔為你收集整理的Python笔记-Flask返回字符串、Json、模板数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。