內容回顧:html
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 }}python
flask回顧:mysql
# 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"redis
1.DBUtils 數據庫鏈接池sql
首先下載插件pip install DBUtils或者python3 -m pip install DBUtils,數據庫
而後運行mysql數據庫,在mysql新建立一個數據庫,並建立一個新的表,錄入數據。json
import pymysql from DBUtils.PooledDB import PooledDB POOL = 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' )
也能夠查詢單條信息:flask
也能夠插入一條數據:瀏覽器
2.Flask請求上下文,應用上下文session