<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表格標(biāo)簽</title>
</head>
<body>
<table border='1' width='50%' cellpadding='0' cellspacing='0' align='center'>
<caption>學(xué)生成績表</caption>
<tr>
<th>學(xué)號</th>
<th>姓名</th>
<th>成績</th>
</tr>
<tr>
<td>1</td>
<td>laoda</td>
<td>95</td>
</tr>
<tr>
<td>2</td>
<td>laoer</td>
<td>98</td>
</tr>
<tr>
<td>3</td>
<td rowspan='2' >laoer</td>
</tr>
<tr>
<td>4<td/>
</tr>
</table>
</body>
</html>
1.1ul無序列表標(biāo)簽
基本結(jié)構(gòu):
<ul>
<li>國際新聞</li>
<li>國內(nèi)新聞</li>
<li>軍事熱點</li>
<li>數(shù)碼、科技</li>
<li>時裝</li>
</ul>
注意:
1.ul標(biāo)簽和li標(biāo)簽是組合使用。
2.li標(biāo)簽里面可以添加任何內(nèi)容,或者嵌套標(biāo)簽也可以
3.無序列表會自動生成一個小圓點,一般在實際應(yīng)用中會去掉。
1.2有序列表ol
語法:定義子項順序的。一般有編號(數(shù)字或字母或希臘字母)
<ol>
<li>小西瓜</li>
</ol>
注意:
有序列表type屬性的幾個值:
A表示顯示的編號是大寫字母
a表示編號是小寫字母
I表示編號是羅馬數(shù)字
i表示編號是小寫羅馬數(shù)字
默認(rèn)情況下顯示的是數(shù)字
實例:
<ol type="1" start="5">
<li><a href="">小蘋果</a></li>
<li><a href="">小西瓜</a></li>
<li><a href="">草莓</a></li>
<li><a href="">西紅柿</a></li>
<li><a href="">獼猴桃</a></li>
<li><a href="">藍(lán)莓</a></li>
</ol>
<ol type="a" >
<li><a href="">小蘋果</a></li>
<li><a href="">小西瓜</a></li>
<li><a href="">草莓</a></li>
<li><a href="">西紅柿</a></li>
<li><a href="">獼猴桃</a></li>
<li><a href="">藍(lán)莓</a></li>
</ol>
<ol type="A">
<li><a href="">小蘋果</a></li>
<li><a href="">小西瓜</a></li>
<li><a href="">草莓</a></li>
<li><a href="">西紅柿</a></li>
<li><a href="">獼猴桃</a></li>
<li><a href="">藍(lán)莓</a></li>
</ol>
<ol type="i">
<li><a href="">小蘋果</a></li>
<li><a href="">小西瓜</a></li>
<li><a href="">草莓</a></li>
<li><a href="">西紅柿</a></li>
<li><a href="">獼猴桃</a></li>
<li><a href="">藍(lán)莓</a></li>
</ol>
<ol type="I">
<li><a href="">小蘋果</a></li>
<li><a href="">小西瓜</a></li>
<li><a href="">草莓</a></li>
<li><a href="">西紅柿</a></li>
<li><a href="">獼猴桃</a></li>
<li><a href="">藍(lán)莓</a></li>
</ol>
2.3定義列表dl
什么是定義列表
dl標(biāo)簽定義了定義列表,通常用于名稱的解釋或者概念的定義。每個子項分為2個部分,一部分負(fù)責(zé)定義,另一部分負(fù)責(zé)解釋
<dl>
<dt>計算機資料</dt>
<dd>H5前端開發(fā)</dd>
<dd>JavaScript</dd>
<dd>NodeJS入門到精通</dd>
<dd>PhP語法入門到精通</dd>
<dt>文學(xué)書籍</dt>
<dd>詩歌</dd>
<dd>小說</dd>
<dd>散文</dd>
</dl>
<!--
dl:定義了一個定義列表
dt:術(shù)語部分
dd:對術(shù)語或項目進(jìn)行解釋的部分
-->
2.1表格的使用
(1)table標(biāo)簽
(2)表格行、列的合并
<table>:定義表格
<tr>:定義表格的行
<td>:定義表格的列
<th> :定義表格表頭單元格
<thead>:定義表格中表頭的內(nèi)容
<tbody>:定義表格中的主體內(nèi)容
<tfoot>:定義表格中的腳注
<caption>:定義表格標(biāo)題
<!--
cellspacing:單元格與單元格之間的距離
cellpadding:內(nèi)容與單元格之間的距離
border:單元格邊框?qū)挾?/p>
bgcolor:設(shè)置背景色
-->
<table width="360" border="1" cellspacing="0" cellpadding="0" align="center" >
<tr bgcolor="aqua"><th>學(xué)號</th><th>姓名</th></tr>
<tr align="center"><td>001</td><td>張三</td></tr>
<tr align="center"><td>002</td><td>李四</td></tr>
<tr align="center"><td>003</td><td>王五</td></tr>
<tr align="center"><td>004</td><td>趙六</td></tr>
</table>
大家好,我是小雨!
今天我們要介紹的知識點是 Django 中的 Templates,它也是 MVT 結(jié)構(gòu)中的 T,Templates 可以叫它模板。Templates 究竟用來做什么,通過這一節(jié)的介紹,希望大家有一個基本的了解。
在上一節(jié)當(dāng)中,我們在瀏覽器中看到的是 HTTPResponse 返回的字符串,而這一節(jié)我們講的 templates 最直接的功能是把前端的 html 頁面顯示出來,這樣用戶就能看到清晰美觀的界面。那么 Django 是如何加載 html 網(wǎng)頁的呢?下面介紹兩種方式:
我們提前準(zhǔn)備好一個 html 頁面index.html,然后為了方便 views 中能成功訪問到這個index.html,需要在settings.py中將templates文件夾的路徑聲明清楚。在 TEMPLATES 列表字典的DIRS屬性下添加一下內(nèi)容:
os.path.join(BASE_DIR, 'templates') # 如果缺少os模塊記得導(dǎo)入
render_to_string 的使用方法: 先使用from django.templates.loader import render_to_string導(dǎo)入模塊,然后給 render_to_string 傳入 html 文件后,最后將其以 HttpResponse 的方式返回出去即可。
from django.http import HttpResponse
from django.templates.loader import render_to_string
def index(request):
html = render_to_string("index.html")
return HttpResponse(html)
這里我們需要從django.shortcut導(dǎo)入 render 模塊,一般情況下 Django 會默認(rèn)導(dǎo)入這個模塊,views 中如果沒有的話就手動導(dǎo)入一下:
from django.shortcut import render
接著 render 中傳入 html 文件,記得添加 request 參數(shù),然后使用 return 返回 render 即可:
def index(request):
return render(request,'index.html')
最后記得在 urls.py 中將路徑對應(yīng)的 views.index 添加進(jìn)來就可以了。
我們要知道 templates 作為 MVT 架構(gòu)中的 T,不僅僅是加載前端中的頁面,它還包含一種模板語言,能夠在 htmlL 語言中實現(xiàn)邏輯控制(條件選擇、循環(huán)),相對于 JavaScript 會更加的靈活。
常用的python模板語言引擎
接下來,我們就以一個簡單的例子來介紹 Django 模板語言的使用。
我們有一個存儲學(xué)生信息的Students.csv文件,包含了學(xué)號、姓名、性別、手機號碼、出生日期、電子郵箱等信息。
319001,趙一,男,1998/12/27,18706012232,532211428@qq.com,北京市海淀區(qū)頤和園路5號,342622199801144314,2019/9/1,計算機,趙一,13655512212
319002,錢二,女,1995/10/25,13459732456,572501101@qq.com,北京市海淀區(qū)雙清路30號,342622199709066819,2019/9/1,物聯(lián)網(wǎng),王二,13856909992
這里數(shù)據(jù)其實有很多條,為了減少篇幅我們就羅列出兩條。
現(xiàn)在需要將這些數(shù)據(jù)加載到前端的 html 頁面中,通過 tabel 標(biāo)簽顯示出來。
(1)準(zhǔn)備
首先用 pycharm 新建一個 Django 項目,為了方便使用直接把 application 和 templates 文件夾都建好。
pycharm新建Django項目
在 templates 中新建一個 index.html 文件,寫好用于顯示學(xué)生信息的標(biāo)題以及表格框架。
<div id="title">學(xué)生信息列表</div>
<div id="table_main">
<table border="1">
<thead>
<tr>
<th>學(xué)號</th>
<th>姓名</th>
<th>性別</th>
<th>出生日期</th>
<th>手機號碼</th>
<th>電子郵箱</th>
<th>家庭地址</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
(2)views 設(shè)置
為了讀取 csv 文件,寫一個read_form_file方法,將文件中的信息存儲在 Students 列表中。
def read_from_file(path:str):
students = []
try:
with open(path, mode="r", encoding="UTF-8") as fd:
# 讀取當(dāng)前行
current_line = fd.readline()
# 判斷當(dāng)前行是否為空
while current_line:
temp_line = current_line.split(",")
students.append(temp_line)
current_line = fd.readline()
return students
except Exception as e:
raise e
接下來,我們定義一個 student 方法,用來顯示 html 內(nèi)容給前端,這里面我們還是使用 render 將獲取的數(shù)據(jù)傳遞給前端頁面index.html。
注意使用 context 參數(shù),它的值我們設(shè)置為一個字典,鍵設(shè)置為 DTL 模板語言中要引用的變量,值設(shè)置為本地獲取的數(shù)據(jù)
def student(request):
students = read_from_file(r"D:\Python\Project\Demo\Student.csv")
return render(request, "index.html", context={"all_student":students})
(3)DTL 模板語言
在index.html中,表格內(nèi)容的填充,我們通過 DTL 模板語言來完成。在 HTML 代碼中要插入 DTL 模板語言,需要使用{%語句%},for 語句還要使用{% endfor %}來結(jié)尾,在語句內(nèi)部調(diào)用變量列表中的元素也是直接使用變量名.數(shù)字并且需要包裹兩層大括號。這里我們以循環(huán)輸出 student 列表中的各個元素為例,其 DTL 模板語言的寫法如下:
{% for student in all_student %}
<tr>
<td>{{ student.0 }}</td>
<td>{{ student.1 }}</td>
<td>{{ student.2 }}</td>
<td>{{ student.3 }}</td>
<td>{{ student.4 }}</td>
<td>{{ student.5 }}</td>
<td>{{ student.6 }}</td>
</tr>
{% endfor %}
(4)urls 配置
接下來配置路由,添加 student 路徑
path('student/',app01_views.student),
(5)運行效果
一切準(zhǔn)備就緒后,執(zhí)行python manage.py runserver,在瀏覽器中打開127.0.0.1:8000/student效果如下:
數(shù)據(jù)被成功加載
不能與服務(wù)器做動態(tài)交互的文件叫靜態(tài)文件; 對于 HTML 中的圖片、視頻、css、js 這些都屬于靜態(tài)文件。Django 對于這些文件有一套明確的管理機制。 如果我們只遵循 HTML 的規(guī)范去加載靜態(tài)文件,在 Django 中是顯示不出來的。
Django無法加載靜態(tài)文件
為了解決這個問題,我們首先要確保配置文件 settings.py 中 INSTALLED_APP 下靜態(tài)文件模塊有被加載到:
'django.contrib.staticfiles', # 必須確保安裝靜態(tài)模塊
然后使用STATICFILES_DIRS這個全局變量來存儲靜態(tài)文件的路徑。
STATICFILES_DIRS = [
os.path.join(BASE.DIR, 'static'),
]
然后在 html 中開始加上模板語言,如果讓 html 引用靜態(tài)文件,那就需要先導(dǎo)入靜態(tài)模塊。在 html 語句第一行寫上
{% load static %}
對于要引用靜態(tài)圖片的標(biāo)簽路徑使用{% static "路徑文件名 "%},如:
<img src="{% static "logo.png"%}">
這樣 Django 就能顯示靜態(tài)文件圖片了。
靜態(tài)圖片被正確顯示了
一般情況下,static 文件夾我們會放在app目錄下。這時候訪問靜態(tài)文件,則需要在STATICFILES_DIRS的 os.path.join 參數(shù)中再多添加一個子文件夾名,可以寫成:
STATICFILES_DIRS = [
os.path.join(BASE.DIR, 'app01','static'),
]
在 Django 配置的文件 settings.py 中有一個全局變量STATIC_URL,它定義了一個給外界用戶直接訪問靜態(tài)文件的路徑。 上面我們添加在標(biāo)題中的 logo.png,我們可以在瀏覽器中直接通過127.0.0.1:8000/student/static/logo.png來訪問。
直接訪問靜態(tài)文件
為了更好地鞏固 Django 加載靜態(tài)文件的知識,我們做一個小案例。學(xué)生信息管理系統(tǒng)的登錄頁面。有顯示圖片 logo,有用戶名和密碼的輸入框,并且有提交按鈕。當(dāng)點擊按鈕時,會彈出提示表示登錄成功。
這里簡單說明一下:
界面采用了 Bootstrap 框架,登錄按鈕的功能函數(shù)用了 jQuery 腳本。關(guān)于前端的知識我們這里就不做過多介紹了。
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<!--加載外部的CSS文件-->
<link type="text/css" rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
<link type="text/css" rel="stylesheet" href="{% static "css/basic.css" %}">
<!--加載外部的js文件-->
<script src="{% static "js/jquery.min.js" %}"></script>
<script src="{% static "js/login.js" %}"></script>
</head>
<body>
<div class="container">
<div id = "login">
<form class="form">
<h2><img src="{% static "imags/mainlogo.png" %}"></h2>
<h2>學(xué)生信息管理系統(tǒng)登錄</h2>
<hr>
<hr>
<div class="form-group">
<label for="user">用戶名:</label>
<input type="text" class="form-control" placeholder="請輸入用戶名" id="user">
</div>
<div class="form-group">
<label for="password">密碼</label>
<input type="password" class="form-control" placeholder="請輸入密碼" id="password">
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox">請記住我
</label>
</div>
</div>
<button id="submit01" class="form-control">提交</button>
</form>
</div>
</div>
</body>
</html>
將以上 html 文件保存在 templates 中,在 views 中配置好方法,urls 中配置好路由,最后在 settings 中配置好靜態(tài)文件路徑,最后啟動 Django 服務(wù)器,頁面顯示效果如下:
登錄頁面案例演示
本節(jié),我們介紹了 Django 中 templates 的知識,Django 加載 HTML 方法、模板的語言的使用,以及加載靜態(tài)文件的方法。希望大家多多練習(xí),才能鞏固所學(xué)的知識。下一節(jié)我們將繼續(xù)介紹 Django 中 URL 跳轉(zhuǎn)的知識!
感謝大家的閱讀!
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。