日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python flask 学习与实战

發布時間:2025/3/14 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python flask 学习与实战 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

路由的另一種表示,在類視圖中會使用

from flask import Flask, make_responseapp = Flask(__name__) def hello():return "Hello World!"app.add_url_rule('/hello', view_func = hello) if __name__ == '__main__':app.run(debug=True)

視圖函數與普通函數的區別

這里是返回 status code, content-type(在headers中默認是text/html),所以當return ''時無回顯,如下面,訪問這個,會跳轉到百度

from flask import Flask, make_responseapp = Flask(__name__) def hello():# status code# content-type(http headers text/html)headers = {'content-type':'text/plain','location':'http://baidu.com'}#response = make_response('<html></html>', 301)#response.headers = headers#return responsereturn '<html></html>', 301, headers#return "<html></html>" def helloo():return "Hello World!"app.add_url_rule('/hello', view_func = hello) if __name__ == '__main__':app.run(debug=True)

實戰

編寫一個書籍搜索,我這里先實現搜索分類,書籍搜索含關鍵字和isbn編號搜索,isbn分為isbn10 isbn13 前者有'-',0-9組成,后面有0-9數字組成

from flask import Flaskapp = Flask(__name__)@app.route('/book/search/<q>/<page>') def search(q, page):"""q (isbn13 0-9 isbn10'-')page"""isbn_or_key = 'key'if len(q) == 13 and q.isdigit():isbn_or_key = 'isbn'short_q = q.replace('-', '')if '-' in q and len(short_q) == 10 and short_q.isdgit:isbn_or_key = 'isbn'pass if __name__ == '__main__':app.run()

轉載于:https://www.cnblogs.com/spark-xl/p/9162077.html

總結

以上是生活随笔為你收集整理的python flask 学习与实战的全部內容,希望文章能夠幫你解決所遇到的問題。

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