访问基本模板中的上下文字段

Access context fields in base template

我有以下基本模板base.template.html

{{ a.b }}

和以下衍生模板derived.template.html

{% extends "base.template.html" %}
{% import "base.template.html" as base %}

我渲染derived.template.html如下

return flask.render_template("derived.template.html", { "a": { "b":1 }})

我得到一个错误

jinja2.exceptions.UndefinedError

UndefinedError: 'a' is undefined

但是,如果base.template.html如下

{{ a }}

一切正常。

为什么会这样?我该如何解决?

从您的 derived.template.html 文件中删除导入行。

{% import "base.template.html" as base %}