flask的蓝图设计
flask的蓝图
蓝图的作用以及结构(结构的目录需要自己定义)
蓝图的作用:规范文件的目录(可以实现多个app,如admin的APP,user的APP)
比如:
-proj_flask
-proj_flask
_admin
-static
-templates
-__init__.py
-views.py
-user
-static
-templates
-__init__.py
-views.py
-user.py
-blog.py
-__init__.py
-run.py
启动文件run.py
# 启动文件
from pro_flask import app
if __name__ == '__main__':
app.run()
user.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Blueprint
user = Blueprint('us', __name__)# 申请一个蓝图,'user'表示当前user文件的蓝图,
# __name__表示当前的启动文件
# 给当前所在的蓝图添加
@user.before_request
def befor1():
print("我是us的befor1")
@user.route("/user")
def index():
return "ok"
blog.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Blueprint,url_for
blog = Blueprint('blog', __name__)# 申请一个蓝图,'blog'表示当前blog文件的蓝图,
# __name__表示当前的启动文件
# 给当前所在的蓝图添加
@blog.before_request
def befor1():
print("我是blog的befor1")
@blog.route("/blog")
def index():
print(url_for("us.index"))
return "blog"
proj_flask/init.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask
app = Flask(__name__,template_folder='templates',static_folder='statics',static_url_path='/static')
from .views.account import account
from .views.blog import blog
from .views.user import user
# 给所有的蓝图添加
@app.before_request
def befor1():
print("我是app的before_request")
# 注册蓝图
app.register_blueprint(account)
app.register_blueprint(blog)
app.register_blueprint(user)
Only you can control your future
You're not alone. You still have family,peopel who care for you and want to save you.