flask模板继承教程 [英] flask Template Inheritance tutorial
问题描述
我正在运行Flask教程,但似乎无法使模板继承正常工作.下面是我的代码示例
I am running through the flask tutorial and I can not seem to get the Template Inheritance to work. below are the examples of my code
我的base.html是:
My base.html is:
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<link rel="stylesheet" href="style.css" />
<title>{% block title %}{% endblock %} - My Webpage</title>
{% endblock %}
</head>
<body>
<div id="content">{% block content %}{% endblock %}</div>
<div id="footer">
{% block footer %}
© Copyright 2008 by <a href="http://domain.invalid/">you</a>.
{% endblock %}
</div>
</body>
</html>
我的孩子的温度是:
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
<style type="text/css">
.important { color: #336699; }
</style>
{% endblock %}
{% block content %}
<h1>Index</h1>
<p class="important">
Welcome to my awesome homepage.
</p>
{% endblock %}
我的烧瓶脚本是:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template("base.html")
if __name__ == "__main__":
app.debug = True
app.run()
关于如何合并子模板,我应该做些特别的事情吗?还是应该以其他方式呈现基本模板?
Is there something specifically I should be doing regarding how I am incorporating the child template? Or should I be rendering the base template differently?
推荐答案
Jinja extends
(表面上)类似于Python子类.在实例化父类时,不会获得子类的实例,在渲染基础模板时,也不会得到子模板的结果.改为渲染子模板.
Jinja extends
works (superficially) like Python subclassing. You don't get an instance of a subclass when you instantiate a parent class, and you don't get the result of a child template when rendering the base template. Render the child template instead.
return render_template('child.html')
这篇关于flask模板继承教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!