Flask中的多个模板渲染

时间:2018-08-01 08:18:04

标签: python flask jinja2

我需要呈现多个模板,我已经看到了此链接,但是也许我仍然做错了事,

这是base.html

<body>

    <div>
        <p>This is a templete base</p>
    </div>

    {% block content %}{% endblock content %}
    {% include "page_2.html" %}


</body>

page_1.html

{% extends "base.html" -%}
{% block content -%}
<div>
    {{data}}
</div>


{%- endblock content %}

page_2.html

{{abc}}

这是python代码(我假设我做错了什么?)

blah = ['blah', 'blah', ' blah']
abc = ['abc', "abc ?", "abc"]

@app.route('/', methods = ['GET', 'POST'])
def check():
    return render_template("page_1.html", data=blah)

@app.route('/', methods = ['GET', 'POST'])
def wow():
    return render_template("page_2.html", abc=abc)

1 个答案:

答案 0 :(得分:2)

我立即发现两个错误:

1)您在同一条路线下定义了2条路线。

2)Page_1需要变量abc,因为它包含page_2。

更改为:

@app.route('/route_1') # <- from '/'
def check():
    return render_template("page_1.html", data=blah, abc=abc) # <- page 1 inherits a need for 'abc'

@app.route('/route_2') # <- from '/'
def wow():
    return render_template("page_2.html", abc=abc)