藍圖可使咱們的程序更加模塊化,不一樣功能的路由能夠放在不一樣的模塊下,最後集中到啓動類中html
它的做用就是將功能和主服務分開,相似django中咱們建立的每個apppython
按照每部分的功能來組織應用,模板放在同一文件夾,視圖存放同一文件夾git
yourapp/ __init__.py static/ templates/ home/ control_panel/ admin/ views/ __init__.py home.py control_panel.py admin.py models.py
視圖文件中的除了__init__.py,每個文件都是一個了藍圖github
全部的藍圖經過頂級的__init__.py註冊到Flask()
中web
按照每一部分所屬的藍圖來組織應用,管理面板的全部的模板,視圖和靜態文件放在一個文件夾中,用戶控制面板的則放在另外一個文件夾中django
yourapp/ __init__.py admin/ __init__.py views.py static/ templates/ home/ __init__.py views.py static/ templates/ control_panel/ __init__.py views.py static/ templates/ models.py
參考來源出處:參考出處連接json
__init__.pyflask
from flask import Flask from lastday.views.account import account from lastday.views.user import user def create_app(): """ 建立app應用 :return: """ app = Flask(__name__) # 引入配置文件並應用 app.config.from_object("settings.Development") app.register_blueprint(account) app.register_blueprint(user) # @app.before_request # def b1(): # print('b1') # # b1 = app.before_request(b1) app.before_request(b1) # app請求以前的中間件 return app def b1(): print('app_b1')
views/account.py瀏覽器
from flask import Blueprint account = Blueprint('account',__name__) @account.before_request def bb(): print('account.bb') @account.route('/login') def login(): return '登錄'
views/user.pysession
from flask import Blueprint,render_template user = Blueprint('user',__name__) @user.route('/user_list') def user_list(): return render_template('user_list.html')
templates/user_list.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>用戶列表</h1> ... <img src="/static/imgs/teacher.jpg"> </body> </html>
manage.py
from lastday import create_app app = create_app() if __name__ == '__main__': app.run()
setting.py
class Base(object): SECRET_KEY = "fasdfasdf" class Product(Base): pass class Testing(Base): DEBUG = False class Development(Base): DEBUG = True
輸入http://127.0.0.1:5000/login,終端打印:
輸入http://127.0.0.1:5000/user_list,終端打印:
實現基本的增刪改查
目錄結構
data.py
STUDENT = [ {"id":1,"name":"小a","age":"18"}, {"id":2,"name":"小b","age":"38"}, {"id":3,"name":"小c","age":"66"}, ]
manage.py
from student import create_app app = create_app() if __name__ == "__main__": app.run(port=5051,debug=True)
__init__.py
from flask import Flask from views import select from views import add from views import update from views import dels def create_app(): app = Flask(__name__) app.register_blueprint(select.my_list) app.register_blueprint(add.my_add) app.register_blueprint(update.my_update) app.register_blueprint(dels.my_del) return app
selsect.py
from flask import Blueprint from flask import render_template from data import STUDENT my_list = Blueprint("my",__name__,template_folder="../templates") @my_list.route("/list") def lists(): return render_template("list.html", student=STUDENT)
list.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="2px"> <button><a href="/add">添加</a></button> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>options</th> </tr> </thead> <tbody> {% for foo in student %} <tr> <td>{{ foo.id }}</td> <td>{{ foo["name"] }}</td> <td>{{ foo["age"] }}</td> <td><a href="/update/{{ foo.id }}">修改</a>|<a href="/del/{{ foo.id }}">刪除</a></td> </tr> {% endfor %} </tbody> </table> </body> </html>
add.py
from flask import Blueprint from flask import request from flask import render_template from data import STUDENT my_add = Blueprint("add",__name__,template_folder="../templates") @my_add.route("/add",methods=["GET","POST"]) def adds(): if request.method == "POST": add_dic = { "id": request.form["id"], "name": request.form["name"], "age": request.form["age"], } STUDENT.append(add_dic) return render_template("add.html")
add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post"> id:<input type="text" name="id"> <br> 姓名:<input type="text" name="name"><br> 年齡:<input type="text" name="age"><br> <input type="submit" value="添加"> </form> </body> </html>
update.py
#!/usr/bin/env python # -*- coding:utf-8 -*- # author: Learning time:2018/9/7 from flask import Blueprint from flask import request,redirect from flask import render_template from data import STUDENT my_update = Blueprint("my_update",__name__,template_folder="../templates") @my_update.route("/update/<int:nid>",methods=["GET","POST"]) def updates(nid): if request.method == "POST": ids = int(request.form["id"]) add_dic = { "id": ids, "name": request.form["name"], "age": request.form["age"], } for index,dicts in enumerate(STUDENT): if dicts["id"] == ids: STUDENT[index] = add_dic return redirect("/list") # 首次訪問,get請求 for dic in STUDENT: if dic["id"] == nid: return render_template("update.html", student=dic) return render_template("update.html", student=" ")
update.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post"> <input type="text" name="id" hidden value="{{ student.id }}"><br> 姓名:<input type="text" name="name" value="{{ student.name }}"><br> 年齡:<input type="text" name="age" value="{{ student.age }}"><br> <input type="submit" value="修改信息"> </form> </body> </html>
dels.py
from flask import Blueprint from flask import redirect from data import STUDENT my_del = Blueprint("my_del",__name__,template_folder="../templates") @my_del.route("/del/<int:nid>",methods=["GET","POST"]) def dels(nid): for dic in STUDENT: if dic["id"] == nid: STUDENT.remove(dic) return redirect("/list")
效果:
1.Flask路由 1.endpoint="user" # 反向url地址 2.url_address = url_for("user") 3.methods = ["GET","POST"] # 容許請求進入視圖函數的方式 4.redirect_to # 在進入視圖函數以前重定向 5./index/<nid> # 動態參數路由 <int:nid> def index(nid) 6.strict_slashes # 是否嚴格要求路由地址 / 7.defaults={"nid":1} # def index(nid) 2.Flask初始化配置(實例化): 1.template_folder # 指定模板路徑 2.static_url_path # 指定靜態文件目錄的URL地址 3.static_folder # 指定靜態文件目錄路徑 3.Flask對象配置 1.DEBUG #開發模式的調試功能 True False 2.app.config.from_object(class) # 經過對象的方式導入配置 3.secret_key # 開啓session功能的時候須要添加的配置 4.Blueprint 1.將功能和主程序分離,註冊 2.bl = Blueprint("dongdong",__name__) 3.註冊 register_blueprint(bl) 5.send_file jsonify 1.send_file # 打開並返回文件 content-type:文件類型 2.jsonify # 將一個字符串 轉爲JSON格式 加入 content-type:application/json 頭 6.特殊的裝飾器: 1.before_request # 在請求進入視圖函數以前執行的函數(登陸認證) 2.after_request # 在請求響應回瀏覽器以前執行的函數 3.before_first_request # 在第一次請求進入視圖函數以前執行的函數 4.errorheader(404) # 當遇到此類錯誤響應的時候(自定義錯誤頁面) 7.flash 1.flash("msg","tag") # 閃現存儲 2.get_flashed_messages(category_filter=["tag"]) # 閃現取值 只要用到了get_flashed_messages就必定清空flash
詳細介紹可參考連接:https://www.cnblogs.com/wupeiqi/articles/7552008.html