django使用html模板減少代碼代碼解析
看下面兩個頁面:


一個顯示文章列表,一個顯示文章詳細信息,其中的部分內(nèi)容相同,有可以重用的部分。
所有就此例可以設置三個html文件:重用部分,目錄部分,文章部分。
重用部分:
base.html
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="zh-CN">
{% load staticfiles %}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>首頁</title>
<script type="text/javascript" src="{% static "bootstrap/js/jquery-2.0.0.min.js" %}"> </script>
<script type="text/javascript" src="{% static "bootstrap/js/jquery-ui.js" %}"></script>
<link href="{% static " rel="external nofollow" bootstrap/css/bootstrap-combined.min.css" %}" rel="stylesheet" media="screen" >
<script type="text/javascript" src="{% static "bootstrap/js/bootstrap.min.js" %}"s></script>
</head>
<body>
<div class="container-fluid" id="LG">
<div class="row-fluid">
<img src="{% static "img/head1.png" %}" alt="返回主頁">
<div class="span12" >
</div>
</div>
<div class="row-fluid">
<div class="span2">
</div>
<div class="span6">
<ul class="nav nav-tabs">
<li class="active">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
</li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >資料</a>
</li>
<li >
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >信息</a>
</li>
</ul>
{% block context %}
添加內(nèi)容
{% endblock context %}
</div>
<div class="span4">
</div>
</div>
</div>
</body>
</html>
使用{%blockcontext%}{%endblockcontext%}標簽,添加不同內(nèi)容
目錄部分
index.html
{% extends "blog/base.html" %}
{% block context %}
{% if latest_article_list %}
<ul>
{% for article in latest_article_list %}
<li>
<a href="{% url 'blog:detail' article.id %}" rel="external nofollow" >
{{ article.title }} </a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No articles are available.</p>
{% endif %}
{% endblock context %}
使用{%extends"blog/base.html"%}載入模板文件,模板文件的位置為相對于templates的路徑。
文章部分:
detail.html
{% extends "blog/base.html" %}
{% block context %}
<h1>{{ article.title }}</h1>
<p>{{ article.content }}</p>
{% endblock context %}
django文檔地址:http://python.usyiyi.cn/django_182/ref/templates/language.html
總結
以上就是本文關于django使用html模板減少代碼代碼解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Django admin實現(xiàn)圖書管理系統(tǒng)菜鳥級教程完整實例
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
使用Python判斷質(zhì)數(shù)(素數(shù))的簡單方法講解
這篇文章主要介紹了使用Python判斷質(zhì)數(shù)(素數(shù))的簡單方法講解,經(jīng)常被用來做科學計算的Python處理這種小問題當然手到擒來^_-需要的朋友可以參考下2016-05-05
python爬蟲使用requests發(fā)送post請求示例詳解
這篇文章主要介紹了python爬蟲使用requests發(fā)送post請求示例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
Pandas數(shù)據(jù)分組統(tǒng)計的實現(xiàn)示例
對數(shù)據(jù)進行分組統(tǒng)計,主要適用DataFrame對象的groupby()函數(shù),本文就來詳細的介紹下Pandas數(shù)據(jù)分組統(tǒng)計的實現(xiàn),具有一定的參考價值,感興趣的可以了解下2023-11-11
python實現(xiàn)linux下使用xcopy的方法
這篇文章主要介紹了python實現(xiàn)linux下使用xcopy的方法,可實現(xiàn)模仿windows下的xcopy命令功能,需要的朋友可以參考下2015-06-06
示例詳解pyqtgraph繪制實時更新數(shù)據(jù)的圖
PyQtGraph是一個基于PyQt和NumPy的Python庫,它專為實時數(shù)據(jù)可視化而設計,本文通過實例代碼給大家介紹pyqtgraph繪制實時更新數(shù)據(jù)的圖,感興趣的朋友一起看看吧2024-12-12

