所以我对如何用烧瓶构建页面感到有点困惑,而不必说明每个视图.
如何在我要加载的页面上制作蓝色打印?
说这些是我的示例页面
templates/
layout.html
section1/
subsection/index.html
subsection2/index.html
section2
subsection/index.html
childofsubsection/index.html
Run Code Online (Sandbox Code Playgroud)
我想假设我去了example.com/section1/subsection/它会知道寻找相应的页面而不必特别说明它.文档http://flask.pocoo.org/docs/blueprints/非常接近解释这一点,但我仍然有点迷失.
from flask import Flask
from yourapplication.simple_page import simple_page
app = Flask(__name__)
app.register_blueprint(simple_page)
Run Code Online (Sandbox Code Playgroud)
还有,不确定这应该去哪里?看起来它会在application.py中出现,但要求从"yourapplication"导入
非常新的烧瓶而不是python专家.真的只是需要一些减少:)
mde*_*ous 12
如果您想查看使用示例Blueprint,可以查看此答案.
关于问题的"模板自动查找"部分:与文档说明一样,蓝图允许指定要查找静态文件和/或模板的文件夹,这样您就不必指定完整路径render_template()调用中的模板文件,但只有文件名.
如果你希望你的观点"神奇地"知道他们应该选择哪个文件,你必须做一些黑客攻击.例如,一个解决方案可能是在视图上应用装饰器,使其根据函数名称选择模板文件,这样的装饰器将如下所示:
from functools import wraps
from flask import render_template
def autorender(func):
@wraps(func)
def wrapper(*args, **kwargs):
context = func(*args, **kwargs)
return render_template('%s.html' % func.func_name, **context)
return wrapper
Run Code Online (Sandbox Code Playgroud)
那么你只需要在视图中将上下文作为dict返回(如果没有上下文,则返回空的dict):
@my_blueprint.route('/')
@autorender
def index():
return {'name': 'John'} # or whatever your context is
Run Code Online (Sandbox Code Playgroud)
它会自动选择名为的模板index.html.
| 归档时间: |
|
| 查看次数: |
3636 次 |
| 最近记录: |