Flask基礎以及Response三劍客

2022-08-01 18:30:16 字數 975 閱讀 1333

flask的特點:

優點:小而精.三方元件全    缺點: 效能相對較差   因為依賴三方元件所以在更新的時候難免不同步

基礎模板

from

flask import flask"/

")def index():

return

"hello world

"

response三劍客+兩個常用的小兒子

劍客一: httpresponse: return "hello world" 返回字串至客戶端

劍客二: render_template

from

flask import render_template # 先引入

render :

return render_template("

login.html

") #在返回

與django中的 render 使用一致 返回模板由瀏覽器渲染

劍客三: redirect

from

flask import redirect #同樣需要引入

redirect:

return redirect("

/login

") # 302

跳轉,重定向url

第乙個兒子jsonify

from

flask import jsonify

retrun jsonify() #返回json標準的字串

第二個兒子send_file

from

flask import send_file

return

send_file(path)

# 開啟檔案並返回檔案內容(自動識別檔案格式)

Flask基礎搭建

fromflaskimportflask flask name 後面跟function是裝飾器的左右 route 裡面填寫的是頁面,裡也可以寫 user about 這裡就是指的使用者相關頁面,這裡指的是主頁面 defhello world return hello world 返回乙個value給...

Flask 框架基礎

flask依賴兩個外部庫 werkzeug 和 jinja2。werkzeug是乙個wsgi 在web應用和多種伺服器之間的標準python介面 工具集。jinja2負責渲染模板。對於werkzeug本質是socket服務端,其用於接收http請求並對請求進行預處理,然後觸發flask框架,開發人員...

flask使用模版templates以及模版傳參

目錄結構,url for redirent.py 如下 from flask import flask,render template 知識點 使用模版檔案,使用函式render template x.html 知識點 模版檔案傳參 def index return render template ...