0Pricing
Flask Academy · 课时

在子页面中扩展基础模板

使用 extends 并按页面填充块

在子页面中扩展基础模板 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Connect a Page to the Base

A child page does not repeat the skeleton. It declares that it extends the base, then fills only the blocks it needs to change. 🔗

The extends Tag

The first line of a child template uses extends to name its parent layout. This tells Jinja2 to inherit the base before rendering.

{% extends "base.html" %}

extends Must Come First

The extends tag has to be the very first thing in the child file. Putting markup above it breaks inheritance and raises a template error.

Override a Block

To replace a slot, redeclare the block with the same name in the child. Whatever you put inside now shows instead of the base default.

{% block title %}Dashboard{% endblock %}

A Complete Child Page

This child template extends base.html and overrides two blocks. The rest of the page comes straight from the parent layout.

{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block content %}
  <h1>Welcome</h1>
{% endblock %}

Skip Blocks You Like

You only override the blocks you want to change. Any block you leave out keeps the default content defined in the base template.

Reuse Parent Content

Inside an overridden block you can call super() to pull in the parent block content, then add your own around it.

{% block content %}{{ super() }}<p>Extra</p>{% endblock %}

Render the Child, Not the Base

Your view returns the child template path. Flask renders the child, which inherits the base, so you never call base.html directly.

return render_template("home.html")

Block Names Must Match

Override only works when the child block name exactly matches one in the base. A typo silently leaves the parent default in place.

One Base, Many Children

Dozens of pages can extend the same base.html. Each child stays tiny because it only carries the markup unique to that page.

Inheritance Saves You

Change the base header once and every child page updates instantly. That cascade is the real power of template inheritance. 🚀

Quick Check

Where must the extends tag appear in a child template?

Recap: Extending Pages

Child pages extend a base, override matching blocks, and may reuse parent content with super(). One base now powers many pages. 🎯

常见问题解答

「在子页面中扩展基础模板」课时是免费的吗?

是的 — 「在子页面中扩展基础模板」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「在子页面中扩展基础模板」这节课中我会学到什么?

使用 extends 并按页面填充块 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「在子页面中扩展基础模板」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用块定义基础模板
  2. 在子页面中扩展基础模板
  3. 包含可复用的局部模板
  4. 用于重复标记的宏
← 返回 Flask Academy