Django DTL模板语法中的循环
1 from django.shortcuts import render 2 3 def index(request): 4 context={ 5 'books':[ 6 '5年高考3年模拟', 7 '家猪养殖与配种', 8 'Python 3 面向对象编程', 9 'MySQL数据库从删库到跑路' 10 ], 11 'person':{ 12 'username':'randomlee', 13 'age':'25', 14 'height':'180' 15 }, 16 'book2s':[ 17 { 18 'name':'5年高考3年模拟', 19 'author':'黄冈中学', 20 'price':'32' 21 }, 22 { 23 'name':'家猪养殖与配种', 24 'author':'不知道', 25 'price':'40' 26 },{ 27 'name':'Python 3 面向对象编程', 28 'author':'a', 29 'price':'22' 30 },{ 31 'name':'MySQL数据库从删库到跑路', 32 'author':'abc', 33 'price':'33' 34 } 35 ], 36 'comments':[ 37 ' 文章的评论内容' 38 ] 39 } 40 return render(request,'index.html',context)
views.py
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <style> 7 body { 8 text-align: center; 9 background: pink; 10 {#line-height: 100px;#} 11 } 12 td{13 border: 1px solid; 14 padding: 10px; 15 16 } 17 table{18 text-align: center; 19 } 20 </style> 21 22 </head> 23 <body> 24 <ul> 25 26 {% for book in books reversed %} 27 <li>{{ book }}</li> 28 {% endfor %} 29 </ul> 30 <ul> 31 {% for v in person.values %} 32 <li>{{ v }}</li> 33 {% endfor %} 34 {% for k in person.keys %} 35 <li>{{ k }}</li> 36 {% endfor %} 37 {% for k,v in person.items %} 38 <li>{{ k }}/{{ v }}</li> 39 {% endfor %} 40 41 </ul> 42 43 <table> 44 <thead> 45 <tr> 46 <td>从1开始序号</td> 47 <td>从0开始序号</td> 48 <td>反转序号最后一位是1</td> 49 <td>反转序号最后一位是0</td> 50 <td>书名</td> 51 <td>作者</td> 52 <td>价格</td> 53 </tr> 54 </thead> 55 <tbody> 56 57 {% for book2 in book2s %} 58 {% if forloop.first %} 59 {# 是否是遍历的第一行 #} 60 <tr style="background: red" > 61 {% elif forloop.last %} 62 {# 是否遍历的最后一行#} 63 <tr style="background: blue"> 64 {% else %} 65 <tr> 66 {% endif %} 67 <td>{{ forloop.counter }}</td> 68 <td>{{ forloop.counter0 }}</td> 69 <td>{{ forloop.revcounter }}</td> 70 <td>{{ forloop.revcounter0 }}</td> 71 <td>{{ book2.name }}</td> 72 <td>{{ book2.author }}</td> 73 <td>{{ book2.price }}</td> 74 </tr> 75 {% endfor %} 76 77 78 </tbody> 79 </table> 80 81 <ul> 82 {% for comment in comments %} 83 <li>{{ comment }}</li> 84 {% empty %} 85 <li>没有任何评论</li> 86 {% endfor %} 87 88 </ul> 89 90 </body> 91 </html>
index.html
template_for_demo.zip
转载于:https://www.cnblogs.com/randomlee/p/10291487.html
Django DTL模板语法中的循环相关推荐
- Django从理论到实战(part19)--DTL模板语法
学习笔记,仅供参考 参考自:Django打造大型企业官网–Huang Y:官方文档 本系列Blog以应用为主,理论基础部分我在后端专栏的Django系列博客已经写过了,如果有些需要补充的知识点,我会在 ...
- 【Django】模板语法
文章目录 前言 新建一条路由 python不同数据类型填充模板 视图向模板中传递数据 字符串填充模板 列表填充模板 字典填充模板 嵌套数据类型填充模板 模板中的判断语句 模板中的循环语句 综合案例 前 ...
- Django DTL 模板系统的局限与理念
Django DTL 模板系统的局限与理念. 以下内容来源于 Django 1.8 LTS 全解 现在,你已经大致了解了 Django Template Language(DTL),或许该说明一下背后 ...
- Django DTL模板变量使用
Django DTL模板变量使用 在模板渲染的视图函数中,增加一个变量数据给到html模板文件 # views.py from django.shortcuts import render,redir ...
- (js)模板字符串中使用循环遍历数据:
(js)模板字符串中使用循环遍历数据: // 数据格式 let wordList = {id: "2",question: "李四",content: [{ n ...
- django之模板语法
1. 变量 Django 模板中遍历复杂数据结构的关键是句点字符 句点符 views.py def index(request):'''模板语法:渲染变量 -> {{}}1. 深度查询,用的句点 ...
- Django 的模板语法之过滤器
后端朝前端页面传递数据的方式# 第一种return render(request,'index.html',{'n':n})# 第二种return render(request,'index.html ...
- 微信小程序在模板语法中使用indexOf失效问题解决办法
失效问题: 小程序的mastache语法不支持js的方法. 即在页面标签中,使用以下js方法无效: Object.keys() toString() indexOf() 解决办法-wxs: WXS(W ...
- beetl模板引擎中for循环语句(包含select设置默认值)和if判断语句
<#select id="salesOfficesId" name="售楼处" > @for(offices in officesLi ...
最新文章
- Excel中条件格式应用的探讨之突出显示!
- java设计模式:Singleton模式
- (转)Virtual PC 2007虚拟网络设置
- 题解: 区间合并(opj 2-4-7620)
- 数据库oracle修改属性列,Oracle修改表结构
- 程序开发中常用的密码学家的算法推荐清单
- Unreadable Notebook NotJSONError('Notebook does not appear to be JSON: u\'{\\n
- matlab 积分方程组,请问用matlab怎么解这个定积分方程组
- python源码包的安装和卸载
- 台达plc控制伺服电机编程实例_三菱PLC控制伺服电机得编程实例
- 基于Struts2和hibernate的WebSocket聊天室的实现教程五:聊天机制
- Markdown语法图文全面详解(10分钟学会)
- 了解、熟悉、精通 的三种并代表什么意思
- Gameplay Ingredients
- HITNet: Hierarchical Iterative Tile Refinement Network for Real-time Stereo Matching--Google
- boost::string_algo库详解
- 高光谱图像去噪相关资源汇总(常用对比算法+数据集+评价指标)
- 开发一个全功能的 Word Add-in
- JAVA 短链码生成工具类
- 微信小程序wepy自定义card控件封装
热门文章
- sql中union 和 union all的区别
- 【BASIS系列】SAP Basis系统管理中重置用户缓冲哪些需要注意
- Analysis of the Clustering Properties of the Hilbert Space-Filling Curve 论文笔记
- Flink Pre-defined Timestamp Extractors / Watermark Emitters(预定义的时间戳提取/水位线发射器)...
- bzoj2333 [SCOI2011]棘手的操作(洛谷3273)
- android singleInstance返回问题
- python批量分析表格_示例python 批量操作excel统计销售榜品牌及销售额
- Python之再说编码
- GoWorld – 用Golang写一个分布式可扩展、可热更的游戏服务器
- javascript对象、类与原型链