1. 程式人生 > >python之django母板頁面

python之django母板頁面

內置 all 模板 table actual template === char hover

其實就是利用{% block xxx %} {% endblock %}的方式定義一個塊,相當於占位。存放在某個html中,比如base.html

然後在需要實現這些塊的文件中,使用繼承{% extends "base.html" %}的方式引入母板文件,然後在{% block xxx %}......{% endblock %}塊定義中實現具體的內容。

base.html示例:註意塊的定義。

技術分享圖片
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標簽*必須*放在最前面,任何其他內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="http://v3.bootcss.com/favicon.ico">
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
    <title>所有的書都在這裏</title>
    {% block page_css %}
    {% endblock %}
    {% block page_js %}
    {% endblock %}

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="http://v3.bootcss.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="http://v3.bootcss.com/examples/dashboard/dashboard.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don‘t actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="http://v3.bootcss.com/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="http://v3.bootcss.com/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

{% include "nav.html" %}

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="{% block book_class %}{% endblock %}"><a href="/book_list/">所有的書 <span class="sr-only">(current)</span></a>
                </li>
                <li class="{% block publisher_class %}{% endblock %}"><a href="/publisher_list/">出版社</a></li>
                <li class="{% block author_class %}{% endblock %}"><a href="/author_list/">作者</a></li>

            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">            
            {% block main_body %}
                {#這裏是每個頁面不同的部分#}
            {% endblock %}
        </div>
    </div>
</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write(‘<script src="http://v3.bootcss.com/assets/js/vendor/jquery.min.js"><\/script>‘)</script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Just to make our placeholder images work. Don‘t actually copy the next line! -->
<script src="http://v3.bootcss.com/assets/js/vendor/holder.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="http://v3.bootcss.com/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
技術分享圖片

某個繼承頁:

技術分享圖片
{# 繼承母板 #}
{% extends ‘base.html‘ %}

{% block book_class %}
    active
{% endblock %}
{% block page_css %}
    {% load static %}
    <link rel="stylesheet" href="{% get_static_prefix %}book_list_only.css">
{% endblock %}

{#把自己頁面的內容,填入母板裏面相應的位置#}
{% block main_body %}
    <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">我是自定義內容,用來替換母板中指定的位置</h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-8"> </div>
                        <div class="col-md-4"><a href="/add_book/" class="btn btn-primary"><i class="fa fa-plus-square"></i> 添加新書</a>
                            <a href="/add_publisher/" class="btn btn-success"><i class="fa fa-plus-square"></i> 添加出版社</a></div>
                    </div>


                    <table class="table table-dark table-hover">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>id</th>
                            <th>名稱</th>
                            <th>出版社</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for b in books %}
                            <tr>
                                <td>{{ forloop.counter }}</td>
                                <td>{{ b.id }}</td>
                                <td>{{ b.title }}</td>
                                <td>{{ b.publisher.name }}</td>
                                <td>
                                    <a href="/del_book/?id={{ b.id }}" class="btn btn-danger"><i class="fa fa-trash fa-fw"></i>刪除</a>
                                    <a href="/edit_book/?id={{ b.id }}" class="btn btn-info"><i class="fa fa-pencil fa-fw"></i>編輯</a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>

                </div>
            </div>
{% endblock %}
技術分享圖片

另一個繼承頁:

技術分享圖片
{% extends "base.html" %}
{% block main_body %}
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">所有的出版社</h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-8"> </div>
                        <div class="col-md-4"><a href="/add_book/" class="btn btn-primary"><i class="fa fa-plus-circle fa-fw"></i>添加新書</a>
                            <a href="/add_publisher/" class="btn btn-success"><i class="fa fa-plus-circle fa-fw"></i>添加出版社</a></div>
                    </div>


                    <table class="table table-dark table-hover">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>id</th>
                            <th>名稱</th>
                            <th>地址</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for p in publisher %}
                            <tr>
                                <td>{{ forloop.counter }}</td>
                                <td>{{ p.id }}</td>
                                <td>{{ p.name }}</td>
                                <td>{{ p.addr }}</td>
                                <td>
                                    <a href="/del_publisher/?id={{ p.id }}" class="btn btn-danger"><i class="fa fa-remove fa-fw"></i>刪除</a>
                                    <a href="/edit_publisher/?id={{ p.id }}" class="btn btn-info"><i class="fa fa-edit fa-fw"></i>編輯</a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>

                </div>
            </div>
  {% endblock %}
{% block publisher_class %}
    active
{% endblock %}
技術分享圖片

完整的練習項目代碼。

1. 母版和繼承
1. 什麽時候用母版?
html頁面有重復的代碼,把它們提取出來放到一個單獨的html文件。
(比如:導航條和左側菜單)
2. 子頁面如何使用母版?
{% extends ‘base.html‘ %} --> 必須要放在子頁面的第一行

母版裏面定義block(塊),子頁面使用block(塊)去替換母版中同名的塊
2. 組件
1. 什麽時候用組件?
重復的代碼,包裝成一個獨立的小html文件。
2. 如何使用?
{% include ‘nav.html‘ %}

3. Django模板語言中關於靜態文件路徑的靈活寫法
1. 利用Django模板語言內置的static方法幫我拼接靜態文件的路徑
{% load static %}
<link href="{% static ‘bootstrap/css/bootstrap.min.css‘ %}" rel="stylesheet">
2. 利用內置的get_static_prefix獲取靜態文件路徑的別名,我們自行拼接路徑
{% load static %}
<link href="{% get_static_prefix %}bootstrap/css/bootstrap.min.css" rel=stylesheet>
3. as語法(一個路徑多次用到,可以使用as保存到一個變量,後面就直接使用變量代替具體路徑)
4. 自定義的simple_tag
比filter高級一點點
它可以接受的參數個數大於2

5. 自定義的inclusion_tag
用來返回一段html代碼(示例:返回ul標簽)

1. 定義階段
在app下面新建templatetags 文件夾(註意是Python包)
新建一個py文件

from django import template
# 生成註冊示例,名字必須是register
register = template.Library()
@register.inclusion_tag("ul.html")
def show_ul(num):
num = 1 if num < 1 else int(num)
data = ["第{:0>3}個數字".format(i) for i in range(1, num+1)]
return {"data": data}

2. 調用階段
{% load xx %}
{% show_ul 10 %}

python之django母板頁面