Python 如何从烧瓶(而不是模板)访问块?
这是我的一般性问题: base.html:Python 如何从烧瓶(而不是模板)访问块?,python,flask,jinja2,Python,Flask,Jinja2,这是我的一般性问题: base.html: <html> <head> {% if title %} <title>{{title}}</title> {% else %} <title>test</title> {% endif %} </head> <body> {% block content %} {% endblock %} &
<html>
<head>
{% if title %}
<title>{{title}}</title>
{% else %}
<title>test</title>
{% endif %}
</head>
<body>
{% block content %} {% endblock %}
</body>
</html>
{%if title%}
{{title}}
{%else%}
测试
{%endif%}
{%block content%}{%endblock%}
如何编写可以直接创建块的函数?可能是这样的:
@app.route("/foo")
def foo():
content = "<h1>this is foo page</h1>"
return render_html("base.html", content = content)
@app.route(“/foo”)
def foo():
content=“这是foo页面”
返回render_html(“base.html”,content=content)
查看:
@app.route("/foo")
def foo():
content = "<h1>this is foo page</h1>"
return render_template("base.html", content = content)
@app.route(“/foo”)
def foo():
content=“这是foo页面”
返回呈现模板(“base.html”,content=content)
base.html:
<html>
<head>
{% if title %}
<title>{{title}}</title>
{% else %}
<title>test</title>
{% endif %}
</head>
<body>
{% block content %}
{% if content %}
{{ content | safe }}
{% else %}
No content provided
{% endif %}
{% endblock %}
</body>
</html>
<html>
<head>
{% if title %}
<title>{{title}}</title>
{% else %}
<title>test</title>
{% endif %}
</head>
<body>
{% block content %}{{ content | safe }}{% endblock %}
</body>
</html>
{%if title%}
{{title}}
{%else%}
测试
{%endif%}
{%block content%}
{%if内容%}
{{content | safe}}
{%else%}
没有提供任何内容
{%endif%}
{%endblock%}
您正在尝试在模板中呈现HTML,而不呈现。默认情况下,Jinja2配置为自动转义插值的所有变量
这意味着
<h1>this is foo page</h1>
这样,您就不会无意中在不想使用HTML的页面中使用HTML。这对于防范攻击非常重要
如果您打算绕过这种自动转义,并有意将HTML插入模板中,那么必须确保您知道自己在做什么——绝不允许未转义的用户输入进入这些变量
有了背景课程和安全警告,如果您知道自己在做什么,您可以在模板中将值显式标记为“安全”,这样它们就不会被转义。只需使用Jinja2的内置过滤器,如下所示:
<body>
{{ content | safe }}
</body>
{{content | safe}}
在您的例子中,我认为您还希望有一个块,这样您就可以使用重写。例如,使用这个完整的示例应用程序
test.py:
import flask
app = flask.Flask(__name__)
@app.route("/foo")
def foo():
content = "<h1>this is foo content</h1>"
return flask.render_template("base.html", content=content)
@app.route("/bar")
def bar():
content = "<h1>this is bar content</h1>"
return flask.render_template("child.html", content=content)
if __name__ == "__main__":
app.run(debug=True)
导入烧瓶
app=烧瓶。烧瓶(\uuuuu名称\uuuuuuu)
@应用程序路径(“/foo”)
def foo():
content=“这是foo内容”
返回flask.render_模板(“base.html”,content=content)
@应用程序路径(“/bar”)
def bar():
content=“这是酒吧内容”
返回flask.render_模板(“child.html”,content=content)
如果名称=“\uuuuu main\uuuuuuuu”:
app.run(debug=True)
模板/base.html:
<html>
<head>
{% if title %}
<title>{{title}}</title>
{% else %}
<title>test</title>
{% endif %}
</head>
<body>
{% block content %}
{% if content %}
{{ content | safe }}
{% else %}
No content provided
{% endif %}
{% endblock %}
</body>
</html>
<html>
<head>
{% if title %}
<title>{{title}}</title>
{% else %}
<title>test</title>
{% endif %}
</head>
<body>
{% block content %}{{ content | safe }}{% endblock %}
</body>
</html>
{%if title%}
{{title}}
{%else%}
测试
{%endif%}
{%block content%}{{content | safe}{%endblock%}
模板/child.html:
{% extends "base.html" %}
{% block content %}
{{ super() }}
<h2>And this bit comes from the child template</h2>
{% endblock %}
{%extends“base.html”%}
{%block content%}
{{super()}}
这个部分来自子模板
{%endblock%}