python?include標(biāo)簽的使用方式及說明
include標(biāo)簽如何使用?
include標(biāo)簽的使用
在講python include標(biāo)簽使用之前,我們新建一個(gè)include_demo項(xiàng)目
截圖如下
項(xiàng)目新建好了,再在templates文件下新建一個(gè)index.html文件,代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{margin:0;padding:0;} ul{list-style: none;} a{text-decoration: none; color: #ffffff;} nav,footer,.main{width:1000px; height:40px; margin:0 auto;} ul{width: 1000px; height:40px; line-height: 40px; background-color:#000000;} li{width:120px; height:40px; line-height:40px; text-align: center; float:left;} .main{clear:both; line-height:40px; background-color:pink;} footer{height:40px; background-color: green;} </style> </head> <body> <nav> <ul> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關(guān)于我們</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >產(chǎn)品中心</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新聞中心</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >服務(wù)宗旨</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >聯(lián)系我們</a></li> </ul> </nav> <div class="main"> 網(wǎng)站首頁(yè)主體部分 </div> <footer> 網(wǎng)站首頁(yè)footer部分 </footer> </body> </html>
然后在include_demo.py頁(yè)面渲染一下index模板文件,代碼如下:
from flask import Flask,render_template app = Flask(__name__) @app.route('/') def hello_world(): return render_template("index.html") if __name__ == '__main__': app.run(debug=True)
運(yùn)行include_demo.py文件,運(yùn)行結(jié)果如下:
在這里主要是為了方便講解include標(biāo)簽,所有沒太注重前端頁(yè)面部分。
通過上面index.html文件就能發(fā)現(xiàn),我將公共和私有代碼部分都在一塊,假設(shè)網(wǎng)站有幾十個(gè)頁(yè)面,我將所有公共代碼和私有代碼
都放一塊,如果有一天要修改某個(gè)公共代碼塊,哪就得修改幾十個(gè)頁(yè)面,那將是件非常麻煩的事。為了方便管理項(xiàng)目,我們將頁(yè)面公共、私有代碼部分抽取出來。
我們新建一個(gè)header.html文件,把css樣式及nav標(biāo)簽內(nèi)容復(fù)制到header.html頁(yè)面中。
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{margin:0;padding:0;} ul{list-style: none;} a{text-decoration: none; color: #ffffff;} nav,footer,.main{width:1000px; height:40px; margin:0 auto;} ul{width: 1000px; height:40px; line-height: 40px; background-color:#000000;} li{width:120px; height:40px; line-height:40px; text-align: center; float:left;} .main{clear:both; line-height:40px; background-color:pink;} footer{height:40px; background-color: green;} </style> </head> <body> <nav> <ul> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關(guān)于我們</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >產(chǎn)品中心</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新聞中心</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >服務(wù)宗旨</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >聯(lián)系我們</a></li> </ul> </nav> </body> </html>
然后新建一個(gè)footer.html文件,把footer標(biāo)簽中的內(nèi)容復(fù)制到該文件中。
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <footer> 網(wǎng)站首頁(yè)footer部分 </footer> </footer> </body> </html>
我們?cè)谶\(yùn)行主app文件,結(jié)果如下:
(^-^),為啥沒有居中,背景色也不見了??因?yàn)槲覀儧]有把樣式引入進(jìn)來(嗯,頁(yè)面太丑了,沒法看了,趕緊關(guān)了?。。?/p>
OK!我們將公共代碼抽取出來后。記得在index.html文件中用include標(biāo)簽導(dǎo)入header、footer代碼塊,代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include "header.html" %} <div class="main"> 網(wǎng)站首頁(yè)主體部分 </div> {% include "footer.html" %} </body> </html>
再運(yùn)行主app文件,結(jié)果如下:
嗯,結(jié)果是不是和之前一樣,對(duì)吧!
通過上面例子,相信大部分小伙伴都明白了include標(biāo)簽的作用及用法??傊痪湓挘琲nclude標(biāo)簽的作用就相當(dāng)于把抽取的代碼復(fù)制到當(dāng)前頁(yè)面中。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何基于windows實(shí)現(xiàn)python定時(shí)爬蟲
這篇文章主要介紹了如何基于windows實(shí)現(xiàn)python定時(shí)爬蟲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python中八種數(shù)據(jù)導(dǎo)入方法總結(jié)
數(shù)據(jù)分析過程中,需要對(duì)獲取到的數(shù)據(jù)進(jìn)行分析,往往第一步就是導(dǎo)入數(shù)據(jù)。導(dǎo)入數(shù)據(jù)有很多方式,不同的數(shù)據(jù)文件需要用到不同的導(dǎo)入方式,相同的文件也會(huì)有幾種不同的導(dǎo)入方式。下面總結(jié)幾種常用的文件導(dǎo)入方法2022-11-11使用實(shí)現(xiàn)python連接hive數(shù)倉(cāng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了使用實(shí)現(xiàn)python連接hive數(shù)倉(cāng)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Python使用日志模塊快速調(diào)試代碼并記錄異常信息
本文詳細(xì)介紹了Python logging日志模塊的使用方法,包括如何在代碼中使用logging記錄調(diào)試信息、如何設(shè)置日志級(jí)別、如何記錄異常信息等。通過本文的指南,讀者可以快速學(xué)會(huì)如何使用logging模塊進(jìn)行調(diào)試,并保留有用的日志信息,便于后續(xù)排查問題和優(yōu)化代碼2023-04-04使用django-crontab實(shí)現(xiàn)定時(shí)任務(wù)的示例
這篇文章主要介紹了使用django-crontab實(shí)現(xiàn)定時(shí)任務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Python數(shù)據(jù)操作方法封裝類實(shí)例
這篇文章主要介紹了Python數(shù)據(jù)操作方法封裝類,結(jié)合具體實(shí)例形式分析了Python針對(duì)數(shù)據(jù)庫(kù)的連接、執(zhí)行sql語(yǔ)句、刪除、關(guān)閉等操作技巧,需要的朋友可以參考下2017-06-06Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝
這篇文章主要為大家介紹了Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝,感興趣的小伙伴們可以參考一下2016-04-04