235.11.flask-蓝图
1.目录结构
2.代码包
- manage.py
from src import app
if __name__ == '__main__':
app.run(debug=True)
- main.py
from flask import Flask
app = Flask(__name__)
app.config.from_object("settings.DevelopmentConfig")
@app.route("/index", methods=["POST", "GET"])
def index():
pass
if __name__ == '__main__':
app.run()
- src/__init__py
# coding=utf-8
"""
蓝图作用:
1.模块划分
2.某个模块路由加前缀
3.蓝图中的before_request装饰器只对当前模块生效
4.蓝图可以实现静态目录 template_folder="", static_folder=""设置
"""
from flask import Flask
app = Flask(__name__)
from views import account
from views import user
from views import admin
app.register_blueprint(account.ac)
app.register_blueprint(user.us)
app.register_blueprint(admin.ad)
- views/init.py
空的
- account.py
from flask import Blueprint
ac = Blueprint('account', __name__)
@ac.route("/login")
def login():
return "login"
@ac.route("/logout")
def logout():
return "logout"
- admin.py
from flask import Blueprint
ad = Blueprint('ac', __name__, url_prefix="/admin", template_folder="", static_folder="")
@ad.before_request
def bf():
print("before request")
@ad.route("/admin")
def admin():
return "admin"
@ad.route("/app")
def app():
return "app"
- user.py
from flask import Blueprint
us = Blueprint('us', __name__)
@us.route("/info")
def info():
return "info"
@us.route("/user")
def user():
return "user"
其他目录文件都是空的
3.项目依赖环境生成
# coding=utf-8
"""
补充:
- 项目依赖 pip3 install pipreqs;
生成依赖文件: pipreqs ./ --force
安装依赖文件: pip3 install -r requirements.txt
- 什么是函数? 什么时方法?
通过对象调用一个函数是他是方法, 方法一般会有一个参数由调用对象传入, 比如self
"""