久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Django模板標簽{% for %}循環,獲取制定條數據實例

瀏覽:140日期:2024-10-07 11:44:38

有時候,為了獲取查詢結果的部分數據,需要對變量進行一些處理,在網上查了一圈,只發現了這兩個方法:

返回查詢結果的切片

在返回給前端的結果中,通過切片來取得想要的數據:

pictures = Post.objects.filter(status=’published’)[:8]

如[:8],但這種操作比較片面,會將返回結果限制住,有時候不利于其他的操作使用

2.使用{% if %}標簽和forloop.counter變量來獲取:

<h3>最新博文</h3> {% for picture in pictures %} {% if forloop.counter > 2 %}{% if forloop.counter < 4 %} <div class='pop-post'><a href='http://m.baoyu77737.com/bcjs/{{ picture.get_absolute_url }}' rel='external nofollow' rel='external nofollow' rel='external nofollow' ><img src='http://m.baoyu77737.com/bcjs/{{ picture.image.url }}' alt='ins-picture'/></a> <div class='info'><h4><a href='http://m.baoyu77737.com/bcjs/{{ picture.get_absolute_url }}' rel='external nofollow' rel='external nofollow' rel='external nofollow' >{{ picture.post_updated }}</a></h4><h3><a href='http://m.baoyu77737.com/bcjs/{{ picture.get_absolute_url }}' rel='external nofollow' rel='external nofollow' rel='external nofollow' >{{ picture.title }}</a></h3> </div> </div>{% endif %} {% endif %} {% empty %} <p>暫無文章!</p> {% endfor %}

通過對forloop.counter的判斷,來確定需要用在前端上的數據,forloop.counter用來統計for循環的次數,從1開始技術,也有forloop.counter0,是從0開始計數

補充知識:python3--django for 循環中,獲取序號

功能需求:在前端頁面中,for循環id會構不成連續的順序號,所以要找到一種偽列的方式來根據數據量定義序號

因此就用到了在前端頁面中的一個字段 forloop.counter,完美解決

<tbody> {% for inrow in insocket_list %} <tr> <!-- 這是序列號(相當于偽列)--> <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模板標簽{% for %}循環,獲取制定條數據實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Django
主站蜘蛛池模板: 邵阳市| 常宁市| 济宁市| 长丰县| 高阳县| 天津市| 睢宁县| 尤溪县| 文水县| 青冈县| 凌云县| 景泰县| 通山县| 屯门区| 博兴县| 太谷县| 都江堰市| 长葛市| 土默特左旗| 财经| 南丰县| 宣威市| 云和县| 沈丘县| 绵竹市| 宁晋县| 阿城市| 长汀县| 观塘区| 岐山县| 甘泉县| 什邡市| 嘉黎县| 叙永县| 桂阳县| 竹溪县| 铜鼓县| 建昌县| 台东市| 化州市| 遂昌县|