看下面兩個頁面:


一個顯示文章列表,一個顯示文章詳細信息,其中的部分內容相同,有可以重用的部分。
所有就此例可以設置三個html文件:重用部分,目錄部分,文章部分。
重用部分:
base.html
<!DOCTYPE html><!DOCTYPE html><html lang="zh-CN">{% load staticfiles %} <head>  <meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge">  <meta name="viewport" content="width=device-width, initial-scale=1">  <title>首頁</title>  <script type="text/javascript" src="{% static "bootstrap/js/jquery-2.0.0.min.js" %}"> </script>  <script type="text/javascript" src="{% static "bootstrap/js/jquery-ui.js" %}"></script>  <link href="{% static " rel="external nofollow" bootstrap/css/bootstrap-combined.min.css" %}" rel="stylesheet" media="screen" >  <script type="text/javascript" src="{% static "bootstrap/js/bootstrap.min.js" %}"s></script></head><body><div class="container-fluid" id="LG">  <div class="row-fluid">    <img src="{% static "img/head1.png" %}" alt="返回主頁">    <div class="span12" >    </div>  </div>  <div class="row-fluid">    <div class="span2">    </div>    <div class="span6">      <ul class="nav nav-tabs">        <li class="active">          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>        </li>        <li>          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >資料</a>        </li>        <li >          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >信息</a>        </li>      </ul>      {% block context %}      添加內容      {% endblock context %}    </div>    <div class="span4">    </div>  </div></div></body></html>使用{%blockcontext%}{%endblockcontext%}標簽,添加不同內容
目錄部分
index.html
{% extends "blog/base.html" %}  {% block context %}      {% if latest_article_list %}      <ul>        {% for article in latest_article_list %}        <li>          <a href="{% url 'blog:detail' article.id %}" rel="external nofollow" >           {{ article.title }} </a>        </li>           {% endfor %}      </ul>      {% else %}        <p>No articles are available.</p>      {% endif %}  {% endblock context %}使用{%extends"blog/base.html"%}載入模板文件,模板文件的位置為相對于templates的路徑。
新聞熱點
疑難解答