python?include標(biāo)簽的使用方式及說(shuō)明
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è)面部分。
通過(guò)上面index.html文件就能發(fā)現(xiàn),我將公共和私有代碼部分都在一塊,假設(shè)網(wǎng)站有幾十個(gè)頁(yè)面,我將所有公共代碼和私有代碼
都放一塊,如果有一天要修改某個(gè)公共代碼塊,哪就得修改幾十個(gè)頁(yè)面,那將是件非常麻煩的事。為了方便管理項(xiàng)目,我們將頁(yè)面公共、私有代碼部分抽取出來(lái)。
我們新建一個(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)來(lái)(嗯,頁(yè)面太丑了,沒法看了,趕緊關(guān)了?。。?/p>
OK!我們將公共代碼抽取出來(lái)后。記得在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ì)吧!
通過(guò)上面例子,相信大部分小伙伴都明白了include標(biāo)簽的作用及用法??傊痪湓?,include標(biāo)簽的作用就相當(dāng)于把抽取的代碼復(fù)制到當(dāng)前頁(yè)面中。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù)
這篇文章主要介紹了Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08
用Python實(shí)現(xiàn)流星雨效果的方法詳解
這篇文章主要為大家介紹了Python實(shí)現(xiàn)流星雨效果的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2021-12-12
python機(jī)器學(xué)習(xí)darts時(shí)間序列預(yù)測(cè)和分析
這篇文章主要介紹了python機(jī)器學(xué)習(xí)darts時(shí)間序列預(yù)測(cè)和分析使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python腳本實(shí)現(xiàn)自動(dòng)將數(shù)據(jù)庫(kù)備份到 Dropbox
本文給大家分享的是作者使用python腳本實(shí)現(xiàn)自動(dòng)備份mysql數(shù)據(jù)庫(kù)到的dropbox網(wǎng)盤的代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2017-02-02
Python使用os模塊和fileinput模塊來(lái)操作文件目錄
這篇文章主要介紹了Python編程中使用os模塊和fileinput模塊來(lái)操作文件的方法,包括獲取路徑和創(chuàng)建愛你刪除目錄等基本操作的例子,需要的朋友可以參考下2016-01-01
python定時(shí)任務(wù)schedule庫(kù)用法詳細(xì)講解
python中有一個(gè)輕量級(jí)的定時(shí)任務(wù)調(diào)度的庫(kù)schedule,下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)schedule庫(kù)用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Python+Pygame實(shí)現(xiàn)之見縫插針游戲的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何利用Python中的Pygame模塊實(shí)現(xiàn)見縫插針游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python游戲開發(fā)有一定幫助,需要的可以參考一下2022-07-07

