Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向烧瓶模板提供标准数据_Python_Templates_Flask - Fatal编程技术网

Python 向烧瓶模板提供标准数据

Python 向烧瓶模板提供标准数据,python,templates,flask,Python,Templates,Flask,我希望能够生成一个基于对象内容的导航栏,基本上有一个导航栏数组或类,其中包含子页面,并生成带有折叠部分的相应导航栏等。我有使用PHP的Laravel的经验,这类似于Flask,但我想不出一个简单的方法来实现。我必须为每个页面提供一组数据对象,因为它是布局的一部分,但我不希望为每个页面专门指定它。有办法做到这一点吗 到目前为止,我只掌握了基础知识、应用程序工厂、视图和蓝图: 工厂 def create_app(): app = Flask(__name__) app.config.

我希望能够生成一个基于对象内容的导航栏,基本上有一个导航栏数组或类,其中包含子页面,并生成带有折叠部分的相应导航栏等。我有使用PHP的Laravel的经验,这类似于Flask,但我想不出一个简单的方法来实现。我必须为每个页面提供一组数据对象,因为它是布局的一部分,但我不希望为每个页面专门指定它。有办法做到这一点吗

到目前为止,我只掌握了基础知识、应用程序工厂、视图和蓝图:

工厂

def create_app():
    app = Flask(__name__)
    app.config.from_pyfile('config.py')

    app.register_blueprint(blueprint_index)

    return app
蓝图

from flask import Blueprint, render_template, session

blueprint_index = Blueprint('simple_page', __name__)


@blueprint_index.route('/')
def index():
    if 'text' in session:
        session['text'] += 1
    else:
        session['text'] = 0
    return render_template('pages/index.html', text=str(session['text']))

忽略我添加到路由中的一点点调试文本。

您可以使用上下文处理器完成此操作。将以下内容添加到蓝图代码块的末尾:

@blueprint_index.context_processor
def inject_text():
    return dict(text=session['text'])
{{text}
现在将在该蓝图的每个模板中可用。

完全是一项工作。