標籤:install query sla 資訊 orm 密碼 default NPU block
內容回顧:
1.Flask-Session
from flask import session
from flask_session import Session
app.config["SESSTION_TPYE"] = "redis"
app.config["SESSTION_REDIS"] = Redis("ip",port,db=1)
# select 1 ----- [1]
Session(app)
session["key"] = "value"
session["key"]
2.Flask CBV
class Index(views.MethodView):
methods = ["GET"]
decorators = [a,b,c]
def get():
return "file"
app.add_url_rule("/",endpoint="index",view_func=Index.as_view(name="index"))
if 1 : endpoint = None = name = "index"
elif 2: endpoint="index" != name="index123"
3.Flask WTForms
from wtforms import Form,validators,widgets
from wtforms.fields import simple
class LgoinForm(Form):
username = simple.StringField(
label = "使用者名稱",
validators=[
validators.DataRequired(message="使用者名稱不可為空"),
],
widget = widgets.TextInput(),
render_kw = {"class":"jinwangba"}
)
def a():
"GET":
return render_template("a.html",fm = LgoinForm())
"POST":
request.form
lfm = LgoinForm(request.form)
lfm.validate() true false
{{ fm.username.label }} {{ fm.username }} {{ fm.username.errors.0 }}
flask回顧:
# 1.路由@app.route(‘/‘,methods=["GET","POST"],endpoint="helloWorld",strict_slashes=True)
# 動態路由參數 /<arg> def viewfunc(arg)
# url_for
# 2.三賤客:
# HTTPResponse return ‘Hello World!‘
# render retrun render_template("index.html")
# redirect retrun redirect("/login")
# 3.request
# 存放資料:
# request.form # 從表單 FormData 擷取到的資料
# request.args # URL傳參時 擷取到的資料
# request.json # Content-Type:application/json 擷取資料
# request.data # b"" Content-Type:xiaowangba 擷取資料
# 請求屬性的
# request.method = "GET"/"POST"
# request.path = "/index"
# request.url = "http://127.0.0.1:5000/index"
# 坑
# request.values.to_dict() 同名被GET(args)覆蓋
# 4.session : Flask-Session
# app.secret_key = "加密字串"
# 存放在瀏覽器Cookies中的被序列化後的session
# session["key"] = "value"
# session["key"]
# session 預設31天的生命週期
# 5.Blueprint
# 藍圖對象 可以理解為 一個不可以被啟動的Flask對象
# bp = Blueprint("bluename",__name__,template_folder="temp_blue")
# @bp.route("/blue")
# app.register_blueprint(blueprint.bp)
# 6.Flask配置
# 1.Flask對象執行個體配置
# class Obj(object):
# DEBUG = True
# SESSION_TYPE = "redis"
# SESSION_REDIS = Redis(db=5)
# app.config.from_object(Obj)
# 2.Flask初始化的配置
# template_folder = 模板檔案存放路徑
# static_folder = "靜態檔案存放路徑"
# static_url_path = "/靜態檔案URL訪問地址"預設是static_folder
# 7.Flask Jinja2
# {{ }} 當引用變數 和 執行函數的時候 , 非邏輯引用
# {% %} if for 邏輯代碼引用
# obj.name obj.get("name") obj["name"] obj_list.1 obj_list[1]
# Markup | safe
# @app.template_global()
# @app.template_filter()
# 8.before_request after_request errorhandler(404,500)
# @app.before_request 多個before_request依次執行
# def be1():
# return None
# @app.after_request 多個after_request,反序執行
# def af1(response)
# return response
# 正常情況: be1-be2-af2-af1
# 異常情況: be1-af2-af1
# @app.errorhandler(404) 重定義錯誤資訊
# 9.flash
# flash("be_fr1")
# get_flashed_messages()
# 10.send_file jsonify
# send_file("檔案路徑") 開啟並返迴文件內容
# jsonify({k:v}) 將字典序列化json,打包一個Content-Type:application/json 返回給用戶端
# 11.Flask CBV
# class Index(views.MethodView):
# methods = ["GET"]
# decorators = [a,b,c]
# def get():
# return "file"
#
#
# app.add_url_rule("/",endpoint="index",view_func=Index.as_view(name="index"))
#
# if 1 : endpoint = None = name = "index"
# elif 2: endpoint="index" != name="index123"
1.DBUtils 資料庫連接池
首先下載外掛程式pip install DBUtils或者python3 -m pip install DBUtils,
然後運行mysql資料庫,在mysql新建立一個資料庫,並建立一個新的表,錄入資料。
import pymysqlfrom DBUtils.PooledDB import PooledDBPOOL = PooledDB( creator=pymysql, # 使用連結資料庫的模組 maxconnections=6, # 串連池允許的最大串連數,0和None表示不限制串連數 mincached=2, # 初始化時,連結池中至少建立的閒置連結,0表示不建立 maxcached=5, # 連結池中最多閑置的連結,0和None不限制 maxshared=3, # 連結池中最多共用的連結數量,0和None表示全部共用。PS: 無用,因為pymysql和MySQLdb等模組的 threadsafety都為1,所有值無論設定為多少,_maxcached永遠為0,所以永遠是所有連結都共用。 blocking=True, # 串連池中如果沒有可用串連後,是否阻塞等待。True,等待;False,不等待然後報錯 maxusage=None, # 一個連結最多被重複使用的次數,None表示無限制 setsession=[], # 開始會話前執行的命令列表。如:["set datestyle to ...", "set time zone ..."] ping=0, # ping MySQL服務端,檢查是否服務可用。 # 如:0 = None = never, # 1 = default = whenever it is requested, # 2 = when a cursor is created, # 4 = when a query is executed, # 7 = always host=‘127.0.0.1‘, # 主機 port=3306, # 連接埠 user=‘root‘, # 使用者 password=‘‘, # 密碼 database=‘s12day113‘, # 建立的資料庫的名字 charset=‘utf8‘)
也可以查詢單條資訊:
也可以插入一條資料:
2.Flask請求上下文,應用上下文
flask基礎四