我的 Flask 应用程序布局如下:
myapp/
run.py
admin/
__init__.py
views.py
pages/
index.html
main/
__init__.py
views.py
pages/
index.html
_init_.py文件是空的。admin/views.py文件的内容如下:
from flask import Blueprint, render_template
admin = Blueprint('admin', __name__, template_folder='pages')
@admin.route('/')
def index():
return render_template('index.html')
main/views.py 与 admin/views.py 相似:
from flask import Blueprint, render_template
main = Blueprint('main', __name__, template_folder='pages')
@main.route('/')
def index():
return render_template('index.html')
run.py是:
from flask import Flask
from admin.views import admin
from main.views import main
app = Flask(__name__)
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(main, url_prefix='/main')
print app.url_map
app.run()
现在,如果我访问http://127.0.0.1:5000/admin/
,它会正确地显示admin/index.html。但是,http://127.0.0.1:5000/main/
仍然显示admin/index.html而不是main/index.html。我检查了app.url_map:
<Rule 'admin' (HEAD, OPTIONS, GET) -> admin.index,
<Rule 'main' (HEAD, OPTIONS, GET) -> main.index,
另外,我验证了main/views.py中的index函数按预期被调用。如果我将main/index.html重命名为其他名称,则它可以正常工作。那么,在不重新命名的情况下,如何实现1http://127.0.0.1:5000/main/1显示main/index.html?
template_folder
更改为"admin/pages"
或"main/pages"
呢? - Jan Kaifer