模板的继承,与导入: extends, block

模板的继承

模板的总体固定不变,但是用户点击登录或注册,页面只变化两个模块位置。

Views.py

def home(request):
    return render(request,'home.html')


def login(request):
    return render(request,'login.html')


def reg(request):
    return render(request,'reg.html')

Login.html

{% extends 'home.html' %}

{% block content %}
<h1 class="text-center">注册</h1>
    <form action="" method="post">
        <p>username: <input type="text" name="username" class="form-control"></p>
        <p>password: <input type="text" name="password" class="form-control"></p>
        <input type="submit" class="btn btn-block">
    </form>
    
{% endblock %}
'home.html'点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>
<body>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <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="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<div class="container-fluid">
     <div class="row">
         <div class="col-md-3">
             <div class="list-group">
                  <a href="/home/" class="list-group-item active">
                    首页
                  </a>
                  <a href="/login/" class="list-group-item">登录</a>
                  <a href="/reg/" class="list-group-item">注册</a>
            </div>
         </div>
         <div class="col-md-9">
             <div class="panel panel-primary">
                  <div class="panel-heading">Panel heading without title</div>
                  <div class="panel-body">
                        {#粘贴来的#}
                  </div>
                </div>

                <div class="panel panel-default">
                  <div class="panel-heading">
                    <h3 class="panel-title">Panel title</h3>
                  </div>
                  <div class="panel-body">
                    <!--模板的继承-->
                      {% block content %}
                            <div class="jumbotron">
                                  <h1>Hello, world!</h1>
                                  <p>...</p>
                                  <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
                            </div>
                      {% endblock %}

                  </div>
            </div>
         </div>
     </div>
</div>
</body>
</html>

login.html

{% extends 'home.html' %}

{% block content %}
<h1 class="text-center">注册</h1>
    <form action="" method="post">
        <p>username: <input type="text" name="username" class="form-control"></p>
        <p>password: <input type="text" name="password" class="form-control"></p>
        <input type="submit" class="btn btn-block">
    </form>
    
{% endblock %}

一般情况下末班页面上至少有三块可以被修改的区域

  • css区域
  • html区域
  • js区域
  {% block css %}

	{% endblock %}
  
  {% block content %}

	{% endblock %}
  
  {% block js %}

	{% endblock %}
  # 每一个子页面就都可以有自己独有的css代码 html代码 js代码
"""
一般情况下 模版的页面上划定的区域越多 那么该模版的扩展性就越高
但是如果太多 那还不如自己直接写
"""

模板的导入

"""
将页面的某一个局部当成模块的形式
哪个地方需要就可以直接导入使用即可
"""
{% include 'wasai.html' %}
posted @ 2022-03-03 16:57  谢俊杰  阅读(78)  评论(0编辑  收藏  举报