赞
踩
当前目录文件
./app
├── main (主体模块:主要定义 uri 和 逻辑的 blueprint py 文件)
│ ├── main.py
├── static(静态文件目录:css)
├── templates(模版文件目录:通用 html )
├── app.py
from flask import Blueprint
main = Blueprint('main',__name__)
from . import main
@main.route('/')
def home():
return 'index.html'
from flask import Blueprint
main = Blueprint('main',__name__)
from . import views
from main import main
app.register_blueprint(blueprint=main,url_prefix='/main')
现在的app.py 如下
# /app/app.py from flask import Flask, Response import os # 用户应用的 目录 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) from main.main import app_test app = Flask(__name__) @app.route('/') def index(): return Response('<h1>hello world. Will be replaced by Home</h1>') # register blueprint , set url prefix app.register_blueprint(blueprint=app_test, url_prefix='/api') from main import main app.register_blueprint(blueprint=main,url_prefix='/main') if __name__=='__main__': app.run(debug=True)
上面的代码在启动的时候会报错:
AttributeError: module 'main.main' has no attribute 'name'
当我们点击进入查看源码的时候,Code 如下
if blueprint.name in self.blueprints:
assert self.blueprints[blueprint.name] is blueprint, (
"A name collision occurred between blueprints %r and %r. Both"
' share the same name "%s". Blueprints that are created on the'
" fly need unique names."
% (blueprint, self.blueprints[blueprint.name], blueprint.name)
)
这就是一个Name Collision 的问题
使用了 app/main/__init__.py 和 /app/main.main.py 两个都标识了自己是 main
为了能继续使用当前的例子,注释掉 main.py
新的目录结构如下
./app
├── main (主体模块:主要定义 uri 和 逻辑的 blueprint py 文件)
│ ├── main.py.bak
│ ├── __init__.py
│ ├── views.py
├── static(静态文件目录:css)
├── templates(模版文件目录:通用 html )
├── app.py
修改一下 app/app.py ,把有 conflict 的地方都注释掉
# /app/app.py from flask import Flask, Response import os # 用户应用的 目录 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # from main.main import app_test # conflict app = Flask(__name__) @app.route('/') def index(): return Response('<h1>hello world. Will be replaced by Home</h1>') # register blueprint , set url prefix # app.register_blueprint(blueprint=app_test, url_prefix='/api') # conflict from main import main app.register_blueprint(blueprint=main,url_prefix='/main') if __name__=='__main__': app.run(debug=True)
程序跑起来咯
在main 文件夹中加入2个新的文件 product 和 order
./app
├── main (主体模块:主要定义 uri 和 逻辑的 blueprint py 文件)
│ ├── main.py.bak
│ ├── __init__.py
│ ├── views.py
│ ├── login.py
│ ├── order.py
├── static(静态文件目录:css)
├── templates(模版文件目录:通用 html )
├── app.py
# /app/main/login.py
__author__ = '1346478633@qq.com'
from flask import Blueprint,render_template
login = Blueprint('login',__name__)
@login.route('/')
def show():
# return 'Login'
return render_template('login.html') # add the html pages under templates
# /app/main/order.py
__author__ = '1346478633@qq.com'
from flask import Blueprint
'''
创建一个blueprint对象。第一个参数可看做该blueprint对象的姓名
在一个app里,姓名不能与其余的Blueprint对象姓名重复
第二个参数__name__用作初始化
'''
order = Blueprint('order',__name__)
@order.route('/') #将蓝图对象当做'app'那样使用
def login():
return 'Order'
修改一下 app/app.py ,加入新的blueprint
# /app/app.py from flask import Flask, Response import os # 用户应用的 目录 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # from main.main import app_test # conflict app = Flask(__name__) @app.route('/') def index(): return Response('<h1>hello world. Will be replaced by Home</h1>') # register blueprint , set url prefix # app.register_blueprint(blueprint=app_test, url_prefix='/api') # conflict from main import main app.register_blueprint(blueprint=main,url_prefix='/main') from main.login import login app.register_blueprint(blueprint=login,url_prefix='/login') # 将 main 模块里的蓝图对象account注册到app from main.order import order app.register_blueprint(blueprint=order,url_prefix='/order') # 将 main 模块里的蓝图对象order注册到app if __name__=='__main__': app.run(debug=True)
测试通过
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。