Python 如何使用Flask Blueprint创建索引路由
试图组织我的项目时,我无法访问索引页,例如,当我访问localhost:5000/时,它正在调用posts/index。我想调用/templates/index,但找不到示例 示例结构:Python 如何使用Flask Blueprint创建索引路由,python,flask,Python,Flask,试图组织我的项目时,我无法访问索引页,例如,当我访问localhost:5000/时,它正在调用posts/index。我想调用/templates/index,但找不到示例 示例结构: |--__init__.py /views |--posts.py |--users.py /templates |_/posts |--index.html |--add.html |_/users |--ind
|--__init__.py
/views
|--posts.py
|--users.py
/templates
|_/posts
|--index.html
|--add.html
|_/users
|--index.html
|--add.html
|--index.html
初始化文件,我正在导入蓝图
from website.views import posts
from website.views import users
app.register_blueprint(posts.mod)
app.register_blueprint(users.mod)
视图文件,调用路由。此示例是posts.py
Flask在需要呈现html页面时,默认情况下会查看templates目录 在本例中,您指定了posts/index.html,这意味着它将被转换为模板/posts/index.html。您需要简单地指出,您只需要呈现index.html,这将自动引用模板/index.html 例如,您可以使用
@mod.route('/')
def index():
return render_template('index.html')
但是,您可以在找到更多详细信息。您可以在蓝图注册期间添加前缀 从website.views导入帖子 app.register_blueprintposts.mod,url_前缀='/posts' / @应用程序路径“/” def索引: 返回渲染模板'index.html'
来源:My init文件必须包含@app.route'/'。。。或者此路由应该在某些视图文件中?您可以在当前所在的视图文件中维护它。
@mod.route('/')
def index():
return render_template('index.html')
mod = Blueprint('posts',__name__)
# posts/
@mod.route('/')
def index():
return render_template('posts/index.html')
# posts/add
@mod.route('/add')
def add():
return render_template('posts/add.html')
# posts/edit
@mod.route('/edit')
def edit():
return render_template('posts/edit.html')