蓝图
作用:对flask程序进行目录结构的划分
1. 基本使用
1
2
3
4
5
6
7
8
9
10
11
12
13 | from flask import Flask, Blueprint
admin = Blueprint('admin', __name__)
@admin.route('/')
def index():
return "Admin Index"
app = Flask(__name__)
# 注册蓝图
app.register_blueprint(admin)
|
2. 给路由加前缀
1
2
3
4
5
6
7
8
9
10
11
12 | from flask import Flask, Blueprint
admin = Blueprint('admin', __name__)
@admin.route('/')
def index():
return "Admin Index"
app = Flask(__name__)
app.register_blueprint(admin, url_prefix='/admin')
|
3. 指定蓝图的静态文件和模板文件位置
1
2
3
4
5
6
7
8
9
10
11
12 | from flask import Flask, Blueprint
admin = Blueprint('admin', __name__, static_folder='static', template_folder='templates')
@admin.route('/')
def index():
return "Admin Index"
app = Flask(__name__)
app.register_blueprint(admin, url_prefix='/admin')
|
4.使用蓝图拆分app
目录结构如下
| $ tree flaskproject -L 3
flaskproject
├── admin
│ ├── templates
│ │ └── login.html
│ └── views.py
├── app.py
├── hello.py
├── templates
└── index.html
|
4.1 创建单独的app
目录以及视图函数文件
创建admin
目录
创建视图函数文件views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | #!/usr/bin/env python3
# _*_ coding:utf-8 _8_
# filename: views.py
from flask import Blueprint, render_template
# 不能被run的Flask实例,蓝图名称不能重复,保证在app中时唯一的
admin = Blueprint("admin", __name__, url_prefix='/admin', template_folder="templates")
# url_prefix URL前缀,用于隔离相同URL
@admin.route("/login")
def login():
return render_template('login.html')
|
创建给admin
使用的templates目录及文件templates/login.html
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Login</title>
</head>
<body>
<p>Admin Login</p>
</body>
</html>
|
4.2 在主app.py
中注册蓝图
| from flask import Flask, render_template
from admin.views import admin
app = Flask(__name__)
app.register_blueprint(admin)
@app.route('/')
def index():
return render_template("index.html")
|
| $ curl -s http://10.0.0.128:5000/admin/login
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Login</title>
</head>
<body>
<p>Admin Login</p>
</body>
</html>
|
5.项目目录结构
5.1 简单项目的目录结构
| flaskproject/
├── blog
│ ├── __init__.py
│ ├── static
│ ├── templates
│ └── views
│ ├── admin.py
│ └── user.py
└── manage.py
|
在admin.py
中写蓝图
1
2
3
4
5
6
7
8
9
10
11
12 | #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: admin.py
from flask import Blueprint
admin = Blueprint("admin", __name__, url_prefix='/admin')
@admin.route("/login")
def login():
return "Login"
|
在blog/__init__.py
中创建Flask app
并导入蓝图
| #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: __init__.py
from flask import Flask
from .views.admin import admin
from .views.user import user
app = Flask(__name__)
app.register_blueprint(admin)
app.register_blueprint(user)
|
在manage.py
中启动Flask程序
| #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: manage.py
from blog import app
if __name__ == '__main__':
app.run(host='0.0.0.0')
|
5.2 大型项目的目录结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | flaskproject
├── blog
│ ├── admin
│ │ ├── __init__.py
│ │ ├── static
│ │ ├── templates
│ │ └── views.py
│ ├── __init__.py
│ └── user
│ ├── __init__.py
│ ├── static
│ ├── templates
│ └── views.py
└── manage.py
|
创建子应用目录,在子应用的__init__.py
文件中创建蓝图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: __init__.py
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: __init__.py
from flask import Blueprint
admin = Blueprint('admin',
__name__,
url_prefix="/admin",
template_folder="templates",
static_folder="static")
# 必须等蓝图创建以后再导入视图函数
from . import views
|
创建子应用的视图函数blog/views.py
| #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: views.py
from . import user
@user.route('/')
def index():
return "user index"
|
在blog/__init__.py
实例化Flask并注册蓝图
1
2
3
4
5
6
7
8
9
10
11
12 | #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: __init__.py
from flask import Flask
from .admin import admin
from .user import user
app = Flask(__name__)
app.register_blueprint(admin)
app.register_blueprint(user)
|
在manage.py
中启动Flask程序
| #!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# filename: manage.py
from blog import app
if __name__ == '__main__':
app.run(host='0.0.0.0')
|