上文提到的Blueprint和厉害吧?
可是有个缺点,就是,还不够框架,因为一个功能不可能就一个文件啊?多文件怎么解决?
还和上文项目架构一样
1.新建两个目录,admin,function
2.admin目录下新建三个python文件:__init__.py,super.py,user.py
3.function目录下新建三个pythot文件:__init__.py,general.py,sys.py
4.编写代码:
(1).admin下的__init__.py:
from flask import Blueprint blueprint = Blueprint('admin',__name__) from . import user from . import super
(2).super.py:
from . import blueprint @blueprint.route('/super') def index_super(): return '欢迎登录管理员系统' @blueprint.route('/super/info') def info_super(): return '你的身份为super'
(3).user.py:
from . import blueprint @blueprint.route('/user') def index_user(): return '欢迎登录用户系统' @blueprint.route('/user/info') def info_user(): return '你的身份为user'
(4).function下的__init__.py:
from flask import Blueprint blueprint = Blueprint('function',__name__) from . import general from . import sys
(5).general.py:
from . import blueprint @blueprint.route('/general') def index_general(): return '欢迎使用general功能' @blueprint.route('/general/info') def info_general(): return '你行使的功能为general功能'
(6).sys.py:
from . import blueprint @blueprint.route('/sys') def index_sys(): return '欢迎使用sys功能' @blueprint.route('/sys/info') def info_sys(): return '你行使的功能为sys功能'
(7).app.py(首页):
from flask import Flask from admin import blueprint as blue_1 from function import blueprint as blue_2 app = Flask(__name__) app.register_blueprint(blue_1,url_prefix='/admin') app.register_blueprint(blue_2,url_prefix='/function') @app.route('/') def hello_world(): return '公司系统' if __name__ == '__main__': app.run()
5.以下是项目框架图:
6.打开浏览器,依次输入:
127.0.0.1:5000/
127.0.0.1:5000/admin/super/
127.0.0.1:5000/admin/super/info/
127.0.0.1:5000/admin/user/
127.0.0.1:5000/admin/user/info
127.0.0.1:5000/function/general/
127.0.0.1:5000/function/general/info
127.0.0.1:5000/function/sys/
127.0.0.1:5000/function/sys/info