第 7 章    範本繼承

▸ 基礎範本

main/templates/main/base.html
<!doctype html>
{% load static %}
<html>
<head>
<title>部落格</title>
<meta charset="utf-8">
<link rel="stylesheet" href="{% static 'main/css/main.css' %}">
</head>
<body>
{% include 'main/menu.html' %}
<h2>部落格 -- {% block heading %}{% endblock %}</h2>
{% block content %}{% endblock %}
</body>
</html>

▸ 將所有範本都改為繼承 base.html 範本

main/templates/main/about.html
{% extends 'main/base.html' %}
{% block heading %}關於{% endblock %}
{% block content %}
<p>歡迎來到我的部落格,您可盡情瀏覽並留言。</p>
{% endblock %}
main/templates/main/main.html
{% extends 'main/base.html' %}
{% block heading %}首頁{% endblock %}
{% block content %}
<p>{{ like }}</p>
{% endblock %}
article/templates/article/article.html
{% extends 'main/base.html' %}
{% block heading %}歡迎蒞臨{% endblock %}
{% block content %}
{% endblock %}

▸ 本章完成專案:blog7.zip