老男孩python全栈就业班第9期第4部分django进阶第66天母版和继承的使用及注意事项

母版和继承的使用及注意事项

使用母版和继承的注意事项:

1.{%  extends 'base.html' %} --> 母版文件:base.html要加引号

2.{%  extends 'base.html' %} 必须放在子页面的第一行!!!

3.可以在base.html中定义很多block,通常我们会和额外定义一个 page.css 和 page.js 两个块

4.views.py 相应的函数中返回的是对应的子页面文件不是 base.html

 

实例

1.在项目下进入目录 templates,新建文件 author_list2.html,加入母版调用代码,并把 author_list.html 中非公用部分代码复制到 author_list2.html 中

{% extends "base.html" %}
{% block page-main %}
    <h1 class="page-header">作者管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">作者列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-3 pull-right">
                    <a href="/add_author/" class="btn btn-success pull-right"><i class="fa fa-plus fa-fw"></i>新页面添加</a>
                    <button class="btn btn-success pull-right" data-toggle="modal" data-target="#myModal">新增
                    </button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>ID</th>
                    <th>名字</th>
                    <th>作品</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for author in author_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ author.id }}</td>
                        <td>{{ author.name }}</td>
                        <td>{% for book in author.book.all %}
                            {% if forloop.last %}
                                {{ book.title }}
                            {% else %}
                                {{ book.title }},
                            {% endif %}

                        {% endfor %}
                        </td>
                        <td>
                            <a class="btn btn-danger" href="/delete_author/?id={{ author.id }}"><i
                                    class="fa fa-trash fa-fw"></i>删除</a>
                            <a class="btn btn-info" href="/edit_author/?id={{ author.id }}"><i
                                    class="fa fa-pencil fa-fw"></i>编辑</a>

                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

2.在项目下进入目录 templates,新建文件 publisher_list2.html,加入母版调用代码,并把 publisher_list.html 中非公用部分代码复制到 publisher_list2.html 中

{% extends 'base.html' %}
{% block page-main %}
    <h1 class="page-header">出版社管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">出版社列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-1 pull-right">
                    <button class="btn btn-success" data-toggle="modal" data-target="#myModal">新增</button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>id</th>
                    <th>出版社名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for publisher in publisher_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ publisher.id }}</td>
                        <td>{{ publisher.name }}</td>
                        <td>
                            <a class="btn btn-danger" href="/delete_publisher/?id={{ publisher.id }}">删除</a>
                            <a class="btn btn-info" href="/edit_publisher/?id={{ publisher.id }}">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

3.测试是发现页面左边菜单列表选择对应功能列表时无法实现已选择效果,在项目下进入目录 templates,修改文件 base.html,使用 block 方式,同时修改对应页面

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; 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="https://v3.bootcss.com/favicon.ico">

    <title>BMS-图书管理系统</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://v3.bootcss.com/examples/dashboard/#">BMS-S10</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Dashboard</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Settings</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Profile</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Help</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="Search...">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="{% block publisher_class %}{% endblock %}"><a href="/publisher_list/">出版社列表</a></li>
                <li class="{% block book_class %}{% endblock %}"><a href="/book_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 page-main %}
            {% endblock %}
        </div>
    </div>
</div>


<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title">用户信息</h4>
      </div>
      <div class="modal-body">
        <form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">邮箱</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
</form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary">保存</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

4.在项目下进入目录 templates,修改文件 publisher_list2.html,在文件最后面加入 {% block publisher_class %}

{% extends 'base.html' %}
{% block page-main %}
    <h1 class="page-header">出版社管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">出版社列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-1 pull-right">
                    <button class="btn btn-success" data-toggle="modal" data-target="#myModal">新增</button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>id</th>
                    <th>出版社名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for publisher in publisher_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ publisher.id }}</td>
                        <td>{{ publisher.name }}</td>
                        <td>
                            <a class="btn btn-danger" href="/delete_publisher/?id={{ publisher.id }}">删除</a>
                            <a class="btn btn-info" href="/edit_publisher/?id={{ publisher.id }}">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

{% block publisher_class %}
    active
{% endblock %}

5.在项目下进入目录 templates,修改文件 book_list2.html,在文件最后面加入 {% block book_class %}  

{# 继承母版 #}
{% extends 'base.html' %}

{# 把自己页面的内容 塞到母版里面相应的位置 #}
{% block page-main %}
    <h1 class="page-header">书籍管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">书籍列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-3 pull-right">
                    <a href="/add_book/" class="btn btn-success pull-right">新页面添加</a>
                    <button class="btn btn-success pull-right" data-toggle="modal" data-target="#myModal"><i
                            class="fa fa-plus fa-tw"></i>新增
                    </button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>id</th>
                    <th>书名</th>
                    <th>出版社名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for i in all_book %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ i.id }}</td>
                        <td>{{ i.title }}</td>
                        <td>{{ i.publisher.name }}</td>
                        <td>
                            <a class="btn btn-danger" href="/delete_book/?id={{ i.id }}"><i
                                    class="fa fa-trash fa-tw"></i>删除</a>
                            <a class="btn btn-info" href="/edit_book/?id={{ i.id }}"><i class="fa fa-pencil fa-tw"></i>编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

{% block book_class %}
    active
{% endblock %}

6.在项目下进入目录 templates,修改文件 author_list2.html,在文件最后面加入 {% block author_class %}

{% extends "base.html" %}
{% block page-main %}
    <h1 class="page-header">作者管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">作者列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-3 pull-right">
                    <a href="/add_author/" class="btn btn-success pull-right"><i class="fa fa-plus fa-fw"></i>新页面添加</a>
                    <button class="btn btn-success pull-right" data-toggle="modal" data-target="#myModal">新增
                    </button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>ID</th>
                    <th>名字</th>
                    <th>作品</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for author in author_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ author.id }}</td>
                        <td>{{ author.name }}</td>
                        <td>{% for book in author.book.all %}
                            {% if forloop.last %}
                                {{ book.title }}
                            {% else %}
                                {{ book.title }},
                            {% endif %}

                        {% endfor %}
                        </td>
                        <td>
                            <a class="btn btn-danger" href="/delete_author/?id={{ author.id }}"><i
                                    class="fa fa-trash fa-fw"></i>删除</a>
                            <a class="btn btn-info" href="/edit_author/?id={{ author.id }}"><i
                                    class="fa fa-pencil fa-fw"></i>编辑</a>

                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

{% block author_class %}
    active
{% endblock %}

7.可以在base.html中定义很多block,通常我们会和额外定义一个 page.css 和 page.js 两个块

8.在项目下进入目录 static,添加文件 book_list_only.css

body {
    background-color: lawngreen;
}

9.在项目下进入目录 static,添加文件 author_list_only.js

alert("这是作者列表页面!");

10.在项目下进入目录 templates,修改文件 base.html,添加 {% block page_css %} 和 {% block page_js %}

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; 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="https://v3.bootcss.com/favicon.ico">

    <title>BMS-图书管理系统</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
    {% block page_css %}
    {% endblock %}
</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://v3.bootcss.com/examples/dashboard/#">BMS-S10</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Dashboard</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Settings</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Profile</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Help</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="Search...">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="{% block publisher_class %}{% endblock %}"><a href="/publisher_list/">出版社列表</a></li>
                <li class="{% block book_class %}{% endblock %}"><a href="/book_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 page-main %}
            {% endblock %}
        </div>
    </div>
</div>


<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title">用户信息</h4>
      </div>
      <div class="modal-body">
        <form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">邮箱</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
</form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary">保存</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
{% block page_js %}
{% endblock %}
</body>
</html>

11.在项目下进入目录 templates,修改文件 book_list2.html,在文件中加入 {% block page_css %} 

{# 继承母版 #}
{% extends 'base.html' %}

{# 把自己页面的内容 塞到母版里面相应的位置 #}
{% block page-main %}
    <h1 class="page-header">书籍管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">书籍列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-3 pull-right">
                    <a href="/add_book/" class="btn btn-success pull-right">新页面添加</a>
                    <button class="btn btn-success pull-right" data-toggle="modal" data-target="#myModal"><i
                            class="fa fa-plus fa-tw"></i>新增
                    </button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>id</th>
                    <th>书名</th>
                    <th>出版社名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for i in all_book %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ i.id }}</td>
                        <td>{{ i.title }}</td>
                        <td>{{ i.publisher.name }}</td>
                        <td>
                            <a class="btn btn-danger" href="/delete_book/?id={{ i.id }}"><i
                                    class="fa fa-trash fa-tw"></i>删除</a>
                            <a class="btn btn-info" href="/edit_book/?id={{ i.id }}"><i class="fa fa-pencil fa-tw"></i>编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

{% block book_class %}
    active
{% endblock %}

{% block page_css %}
    <link rel="stylesheet" href="/static/book_list_only.css" />
{% endblock %}

12.在项目下进入目录 templates,修改文件 author_list2.html,在文件中加入 {% block page_js %} 

{% extends "base.html" %}
{% block page-main %}
    <h1 class="page-header">作者管理页面</h1>

    <div class="panel panel-primary">
        <!-- Default panel contents -->
        <div class="panel-heading">作者列表 <i class="fa fa-thumb-tack pull-right"></i></div>
        <div class="panel-body">
            <div class="row" style="margin-bottom: 15px">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for...">
                        <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-md-4 -->
                <div class="col-md-3 pull-right">
                    <a href="/add_author/" class="btn btn-success pull-right"><i class="fa fa-plus fa-fw"></i>新页面添加</a>
                    <button class="btn btn-success pull-right" data-toggle="modal" data-target="#myModal">新增
                    </button>
                </div>

            </div><!-- /.row -->

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>#</th>
                    <th>ID</th>
                    <th>名字</th>
                    <th>作品</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for author in author_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ author.id }}</td>
                        <td>{{ author.name }}</td>
                        <td>{% for book in author.book.all %}
                            {% if forloop.last %}
                                {{ book.title }}
                            {% else %}
                                {{ book.title }},
                            {% endif %}

                        {% endfor %}
                        </td>
                        <td>
                            <a class="btn btn-danger" href="/delete_author/?id={{ author.id }}"><i
                                    class="fa fa-trash fa-fw"></i>删除</a>
                            <a class="btn btn-info" href="/edit_author/?id={{ author.id }}"><i
                                    class="fa fa-pencil fa-fw"></i>编辑</a>

                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <nav aria-label="Page navigation" class="text-right">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

{% block author_class %}
    active
{% endblock %}

{% block page_js %}
    <script src="/static/author_list_only.js"></script>
{% endblock %}

  

 

  

 

posted on 2019-12-06 11:05  herisson_pan  阅读(16)  评论(0)    收藏  举报

导航