Django模板標(biāo)簽{% for %}循環(huán),獲取制定條數(shù)據(jù)實(shí)例
有時(shí)候,為了獲取查詢結(jié)果的部分?jǐn)?shù)據(jù),需要對(duì)變量進(jìn)行一些處理,在網(wǎng)上查了一圈,只發(fā)現(xiàn)了這兩個(gè)方法:
返回查詢結(jié)果的切片
在返回給前端的結(jié)果中,通過(guò)切片來(lái)取得想要的數(shù)據(jù):
pictures = Post.objects.filter(status='published')[:8]
如[:8],但這種操作比較片面,會(huì)將返回結(jié)果限制住,有時(shí)候不利于其他的操作使用
2.使用{% if %}標(biāo)簽和forloop.counter變量來(lái)獲?。?/p>
<h3>最新博文</h3>
{% for picture in pictures %}
{% if forloop.counter > 2 %}
{% if forloop.counter < 4 %}
<div class="pop-post"><a href="{{ picture.get_absolute_url }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="{{ picture.image.url }}" width="100" height="80" alt="ins-picture"/></a>
<div class="info">
<h4><a href="{{ picture.get_absolute_url }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ picture.post_updated }}</a></h4>
<h3><a href="{{ picture.get_absolute_url }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ picture.title }}</a></h3>
</div>
</div>
{% endif %}
{% endif %}
{% empty %}
<p>暫無(wú)文章!</p>
{% endfor %}
通過(guò)對(duì)forloop.counter的判斷,來(lái)確定需要用在前端上的數(shù)據(jù),forloop.counter用來(lái)統(tǒng)計(jì)for循環(huán)的次數(shù),從1開(kāi)始技術(shù),也有forloop.counter0,是從0開(kāi)始計(jì)數(shù)
補(bǔ)充知識(shí):python3--django for 循環(huán)中,獲取序號(hào)
功能需求:在前端頁(yè)面中,for循環(huán)id會(huì)構(gòu)不成連續(xù)的順序號(hào),所以要找到一種偽列的方式來(lái)根據(jù)數(shù)據(jù)量定義序號(hào)
因此就用到了在前端頁(yè)面中的一個(gè)字段 forloop.counter,完美解決
<tbody>
{% for inrow in insocket_list %}
<tr>
<!-- 這是序列號(hào)(相當(dāng)于偽列)-->
<td>{{ forloop.counter }}</td>
<td>{{ inrow.inequip }}</td>
<td>{{ inrow.inmodel }}</td>
<td>{{ inrow.innumber }}</td>
<td>{{ inrow.stocknumber }}</td>
<td>{{ inrow.inusername }}</td>
<td>{{ inrow.inestablishtime }}</td>
<td>{{ inrow.remarks }}</td>
</tr>
{% endfor %}
</tbody>
以上這篇Django模板標(biāo)簽{% for %}循環(huán),獲取制定條數(shù)據(jù)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解用Pytest+Allure生成漂亮的HTML圖形化測(cè)試報(bào)告
這篇文章主要介紹了詳解用Pytest+Allure生成漂亮的HTML圖形化測(cè)試報(bào)告,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python內(nèi)置模塊logging用法實(shí)例分析
這篇文章主要介紹了Python內(nèi)置模塊logging用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python基于logging模塊的日志配置、輸出等常用操作技巧,需要的朋友可以參考下2018-02-02
python執(zhí)行數(shù)據(jù)庫(kù)的查詢操作實(shí)例講解
在本篇文章里小編給大家整理了一篇關(guān)于python執(zhí)行數(shù)據(jù)庫(kù)的查詢操作實(shí)例講解內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2021-10-10
基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的銀行轉(zhuǎn)賬操作
這篇文章主要介紹了基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的銀行轉(zhuǎn)賬操作的相關(guān)資料,需要的朋友可以參考下2016-03-03
Opencv簡(jiǎn)單圖像操作方法實(shí)戰(zhàn)
OpenCV是一個(gè)開(kāi)源的計(jì)算機(jī)視覺(jué)庫(kù),它提供了一系列豐富的圖像處理和計(jì)算機(jī)視覺(jué)算法,這篇文章主要給大家介紹了關(guān)于Opencv簡(jiǎn)單圖像操作方法的相關(guān)資料,需要的朋友可以參考下2024-02-02
解決List.append()?在?Python?中不起作用的問(wèn)題
在?Python?中,我們通常使用?List.append()?方法向列表末尾添加元素,然而,在某些情況下,你可能會(huì)遇到?List.append()?方法不起作用的問(wèn)題,本文將詳細(xì)討論這個(gè)問(wèn)題并提供解決方法,需要的朋友可以參考下2023-06-06

