Django 简明教程

Django - Add Master Template

Django 支持模板继承。Django 中继承的概念与面向对象编程中的继承非常相似。这个想法是,Web 应用程序中每个视图呈现的输出都必须遵循统一的格式或外观,即使每个视图可能呈现不同的模板。

Django supports template inheritance. The concept of inheritance in Django is very similar to inheritance in object-oriented programming. The idea is that the output rendered by each view in a web application must follow a uniform pattern or look, even though each view may render a different template.

假设一个 Django 应用程序有三个使用三个视图注册的 URL 路由。我们希望以这样的方式设计模板,即每个视图都应该有一个页眉、一个页脚以及一个带有链接的侧边栏和显示在其右侧的可变内容。

Suppose a Django app has three URL routes registered with three views. We want to design the template in such a way that each view should have a page header, a footer and a sidebar with links and the variable content displayed to its right.

The Master Template

任何面向对象语言(例如 Python)中的 base class 定义属性和方法,并让继承类可以使用它们。同样,我们需要设计一个 master template ,为其他模板提供整体框架。

A base class in any object-oriented language (such as Python) defines attributes and methods and makes them available to the inherited class. In the same way, we need to design a master template that provides an overall skeleton for other templates.

master template (有时称为“基模板”)连同通用结构也标记了虚拟块。子模板继承通用结构并覆盖这些块以提供各自的内容。这些块用 block – endblock 结构标记。

The master template (sometimes called "base template"), along with the common structure, also marks the dummy blocks. The child template inherits the common structure and overrides the blocks to provide respective contents. Such blocks are marked with "block – endblock" construct.

{% block block_name %}
   ...
   ...
{% endblock %}

主模板可能在不同位置包含多个此类块。每一个都应当提供一个唯一的标识符。

The master template may have more than one such blocks in different places. Each one should be provided a unique identifier.

我们主模板( base.html )的 HTML 代码如下 −

The HTML code for our master template (base.html) is as follows −

<!doctype html>
<html>
<body>
   <!--header-->
   <div style="height:10%;">
      <h2 align="center">My Web Application</h2>
      <hr>
   </div>
   <div style="width:100%;">
      <!—side bar-->
      <div style="width:20%; float:left; border-right-style:groove">
         <ul>
            <b>
               <li><a href="">home</a></li>
               <li><a href="register/">register</a></li>
               <li><a href="login/">login</a></li>
            </b>
         </ul>
      </div>
      <!--contents-->
      <div style="margin-left:21%;">
         <p>
            {% block contents %}
            {% endblock %}
         </p>
      </div>
   </div>
   <br><br><br>
   <!--footer-->
   <hr>
   <div>
      <h4 align="right">All rights reserved</h4>
   </div>
</body>
</html>

主页的模板( index.html )通过标记继承了 base.html

The template for the home page (index.html) inherits this base.html by the tag −

{% extends "base.html" %}

它使用自己的内容填充虚拟内容块 −

It populates the dummy content block with its own content −

<!doctype html>
<html>
<body>
   {% extends "base.html" %}
   {% block contents %}
      <h2 align="center">This is Home page</h2>
   {% endblock %}
</body>
</html>

Define a View

让我们定义一个呈现此模板的视图 −

Let us define a view that renders this template −

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index(request):
   return render(request, "index.html", {})

Register the View

我们还需在 urls.py 中注册该视图 −

We also need to register this view in urls.py

urlpatterns = [
   ...,
   path('home/', views.index, name='home'),
]

当打开 http://localhost:8000/myapp/home URL 时,主页模板将使用 base.html 中设计的页眉、边栏和页脚进行渲染 −

When http://localhost:8000/myapp/home URL is opened, the home page template is rendered with the header, sidebar and footer as designed in base.html

django add master template