【发布时间】:2019-03-24 15:34:09
【问题描述】:
背景
我正在尝试设置一个名称与它所在的文件名匹配的蓝图,这样当我在app.py 中引用它时,我就知道蓝图的来源。这应该是可能的,因为exploreflask 上的示例使用了相同的模式。尽管如此,我还是无法弄清楚如何使用我的结构来完成这项工作。
文件结构
├── app.py
├── frontend
├── __init__.py
└── views
├── home.py
└── __init__.py
示例
frontend/views/home.py
from flask import Blueprint, render_template
home = Blueprint('home', __name__)
home1 = Blueprint('home1', __name__)
前端/视图/__init__.py
from .home import home
from .home import home1
app.py
from flask import Flask
from frontend.views import home
from frontend.views import home1
print (type(home)) --> <class 'function'>
print (type(home1)) --> <class 'flask.blueprints.Blueprint'>
由于home1 正确注册为Blueprint 但home 我不怀疑
有名称冲突,但我不知道如何解决它,尽管调查
this excellent article 关于导入约定。
因此,当我尝试在应用中注册我的蓝图时 这将起作用:
app.register_blueprint(home1, url_prefix='/home1') --> Fine
但这不会:
app.register_blueprint(home, url_prefix='/home')
--> AttributeError: 'function' object has no attribute 'name'
为什么不直接使用 home1 呢?
- 我想了解如何解决冲突
- 我希望能够使用与它们所在的文件名相同的路由名称,如下所示:
frontend/views/home.py
from flask import Blueprint, render_template
home = Blueprint('home', __name__)
@home.route('/')
def home():
pass
【问题讨论】:
-
您是否尝试使用exporeflask 中描述的分区或功能结构?
标签: python flask python-import