欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python之django母板頁(yè)面的使用

 更新時(shí)間:2018年07月03日 08:16:32   作者:楓若雪  
這篇文章主要介紹了python之django母板頁(yè)面的使用,母版頁(yè)用于處理html頁(yè)面相同部分內(nèi)容,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

其實(shí)就是利用{% block xxx %}   {% endblock %}的方式定義一個(gè)塊,相當(dāng)于占位。存放在某個(gè)html中,比如base.html

然后在需要實(shí)現(xiàn)這些塊的文件中,使用繼承{% extends "base.html" %}的方式引入母板文件,然后在{% block xxx %}......{% endblock %}塊定義中實(shí)現(xiàn)具體的內(nèi)容。

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個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
  <meta name="description" content="">
  <meta name="author" content="">
  <link rel="icon"  rel="external nofollow" >
  <link rel="stylesheet"  rel="external nofollow" >
  <title>所有的書都在這里</title>
  {% block page_css %}
  {% endblock %}
  {% block page_js %}
  {% endblock %}

  <!-- Bootstrap core CSS -->
  <link  rel="external nofollow" rel="stylesheet">

  <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <link  rel="external nofollow" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link  rel="external nofollow" 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/" rel="external nofollow" >所有的書 <span class="sr-only">(current)</span></a>
        </li>
        <li class="{% block publisher_class %}{% endblock %}"><a href="/publisher_list/" rel="external nofollow" >出版社</a></li>
        <li class="{% block author_class %}{% endblock %}"><a href="/author_list/" rel="external nofollow" >作者</a></li>

      </ul>
    </div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">      
      {% block main_body %}
        {#這里是每個(gè)頁(yè)面不同的部分#}
      {% 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>

某個(gè)繼承頁(yè):

{# 繼承母板 #}
{% extends 'base.html' %}

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

{#把自己頁(yè)面的內(nèi)容,填入母板里面相應(yīng)的位置#}
{% block main_body %}
  <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title">我是自定義內(nèi)容,用來(lái)替換母板中指定的位置</h3>
        </div>
        <div class="panel-body">
          <div class="row">
            <div class="col-md-8"> </div>
            <div class="col-md-4"><a href="/add_book/" rel="external nofollow" rel="external nofollow" class="btn btn-primary"><i class="fa fa-plus-square"></i> 添加新書</a>
              <a href="/add_publisher/" rel="external nofollow" rel="external nofollow" 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 }}" rel="external nofollow" class="btn btn-danger"><i class="fa fa-trash fa-fw"></i>刪除</a>
                  <a href="/edit_book/?id={{ b.id }}" rel="external nofollow" class="btn btn-info"><i class="fa fa-pencil fa-fw"></i>編輯</a>
                </td>
              </tr>
            {% endfor %}
            </tbody>
          </table>

        </div>
      </div>
{% endblock %}

另一個(gè)繼承頁(yè):

{% 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/" rel="external nofollow" rel="external nofollow" class="btn btn-primary"><i class="fa fa-plus-circle fa-fw"></i>添加新書</a>
              <a href="/add_publisher/" rel="external nofollow" rel="external nofollow" 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 }}" rel="external nofollow" class="btn btn-danger"><i class="fa fa-remove fa-fw"></i>刪除</a>
                  <a href="/edit_publisher/?id={{ p.id }}" rel="external nofollow" 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 %}

完整的練習(xí)項(xiàng)目代碼:day63_base_jb51.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中使用matplotlib繪制熱力圖

    python中使用matplotlib繪制熱力圖

    熱力圖,是一種通過(guò)對(duì)色塊著色來(lái)顯示數(shù)據(jù)的統(tǒng)計(jì)圖表,它通過(guò)使用顏色編碼來(lái)表示數(shù)據(jù)的值,并在二維平面上呈現(xiàn)出來(lái),本文就給大家介紹一下python使用matplotlib繪制熱力圖的方法,需要的朋友可以參考下
    2023-08-08
  • Python struct模塊解析

    Python struct模塊解析

    我們知道python只定義了6種數(shù)據(jù)類型,字符串,整數(shù),浮點(diǎn)數(shù),列表,元組,字典。但是C語(yǔ)言中有些字節(jié)型的變量,在python中該如何實(shí)現(xiàn)呢?這點(diǎn)頗為重要,特別是要在網(wǎng)絡(luò)上進(jìn)行數(shù)據(jù)傳輸?shù)脑挕?/div> 2014-06-06
  • pygame可視化幸運(yùn)大轉(zhuǎn)盤實(shí)現(xiàn)

    pygame可視化幸運(yùn)大轉(zhuǎn)盤實(shí)現(xiàn)

    這篇文章主要介紹了pygame可視化幸運(yùn)大轉(zhuǎn)盤實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 如何利用python讀取micaps文件詳解

    如何利用python讀取micaps文件詳解

    這篇文章主要給大家介紹了關(guān)于如何利用python讀取micaps文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Python?虛擬環(huán)境的價(jià)值和常用命令詳解

    Python?虛擬環(huán)境的價(jià)值和常用命令詳解

    在實(shí)際項(xiàng)目開發(fā)中,我們通常會(huì)根據(jù)自己的需求去下載各種相應(yīng)的框架庫(kù),如Scrapy、Beautiful?Soup等,但是可能每個(gè)項(xiàng)目使用的框架庫(kù)并不一樣,或使用框架的版本不一樣,今天給大家分享下Python?虛擬環(huán)境的價(jià)值和常用命令,感興趣的朋友一起看看吧
    2022-05-05
  • python基礎(chǔ)之函數(shù)

    python基礎(chǔ)之函數(shù)

    這篇文章主要介紹了python的函數(shù),實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下
    2021-10-10
  • 如何測(cè)試Python網(wǎng)站的訪問(wèn)速度,并且優(yōu)化Python網(wǎng)站的性能

    如何測(cè)試Python網(wǎng)站的訪問(wèn)速度,并且優(yōu)化Python網(wǎng)站的性能

    本文使用網(wǎng)絡(luò)工具和Python測(cè)速庫(kù)進(jìn)行測(cè)試Python網(wǎng)站的訪問(wèn)速度,通過(guò)優(yōu)化代碼性能和優(yōu)化服務(wù)器性能以及優(yōu)化數(shù)據(jù)庫(kù)性能等有針對(duì)性地優(yōu)化Python網(wǎng)站的性能
    2024-01-01
  • python爬蟲之selenium庫(kù)的安裝及使用教程

    python爬蟲之selenium庫(kù)的安裝及使用教程

    今天帶大家來(lái)學(xué)習(xí)怎么安裝使用selenium庫(kù),文中有非常詳細(xì)的圖文介紹,對(duì)正在學(xué)習(xí)python爬蟲的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 使用Python腳本將絕對(duì)url替換為相對(duì)url的教程

    使用Python腳本將絕對(duì)url替換為相對(duì)url的教程

    這篇文章主要介紹了使用Python腳本將絕對(duì)url替換為相對(duì)url的教程,腳本代碼非常簡(jiǎn)單,需要的朋友可以參考下
    2015-04-04
  • Python協(xié)程的2種實(shí)現(xiàn)方式分享

    Python協(xié)程的2種實(shí)現(xiàn)方式分享

    在?Python?中,協(xié)程(Coroutine)是一種輕量級(jí)的并發(fā)編程方式,可以通過(guò)協(xié)作式多任務(wù)來(lái)實(shí)現(xiàn)高效的并發(fā)執(zhí)行。本文主要介紹了Python實(shí)現(xiàn)協(xié)程的2種方式,希望對(duì)大家有所幫助
    2023-04-04

最新評(píng)論