• django 模板 变量 过滤器


    模板渲染     语法 {{ 变量 }}  {% 逻辑 %}

    变量

     1 示例
     2 html代码:
     3     <p>{{ num }}</p>
     4     <p>{{ name }}</p>
     5     <p>{{ namelist.2 }}</p>
     6     <p>{{ d1.age }}</p>
     7     <p>{{ a.kind }}</p>
     8     <p>{{ a.eat }}</p>
     9 views.py代码
    10     def index(request):
    11         num = 100
    12         name = 'shige'
    13         name_list = ['大壮','小壮','壮壮']
    14         d1 = {'name':'大壮','age':73,'hobby':'xuefei+xiangxi'}
    15 
    16         class Animal:
    17             def __init__(self):
    18                 self.kind = 'dog'
    19             def eat(self):
    20                 return 'shi'
    21         a = Animal()
    22 
    23         return render(request,'index.html',{'num':num,'name':name,'namelist':name_list,'d1':d1,'a':a})
    24         return render(request,'index.html',locals()) 
    25         locals() 获取函数内部所有变量的值,并加工成{'变量名':'变量值'....}这样一个字典

    过滤器

    内置过滤器

     1 <!-- 过滤器 -->
     2 <!-- 获取数据长度,没参数 -->
     3 <p>{{ name_list|length }}</p>
     4 
     5 <!-- 默认值,有参数,如果一个变量是false或者为空,使用给定的默认值。 否则,使用变量的值。-->
     6 <p>{{ xx|default:'啥也没有' }}</p>
     7 
     8 <!-- 将值格式化为一个 “人类可读的” 文件尺寸 (例如 '13 KB', '4.1 MB', '102 bytes', 等等) -->
     9 <p>{{ movesize|filesizeformat }}</p>
    10 <!--  切片 -->
    11 <p>{{ name|slice:':3' }}</p>
    12 <!--  时间格式化显示 -->
    13 <p>{{ now|date:'Y-m-d' }}</p>
    14 <!--  字符截断 -->
    15 <p>{{ words|truncatechars:'9' }}</p>
    16 
    17 <!--  单词截断 -->
    18 <p>{{ words|truncatewords:'3' }}</p>
    19 
    20 <!-- 移除value中所有的与给出的变量相同的字符串 -->
    21 <p>{{ words|cut:'i' }}</p>
    22 
    23 <!-- 使用字符串连接列表,{{ list|join:', ' }},就像Python的str.join(list) -->
    24 <p>{{ name_list|join:'+' }}</p>
    25 
    26 <!-- 将 字符串识别成标签-->
    27 <p>{{ tag|safe }}</p>
  • 相关阅读:
    关键路径的计算
    JSF简单介绍
    介绍:一款Mathematica的替代开源软件Mathetics
    素材链接
    JSP动作--JSP有三种凝视方式
    【InversionCount 逆序对数 + MergeSort】
    全响应跨设备的Zoomla!逐浪CMS2 x2.0正式公布
    DirectSound的应用
    “海归”首选北上广 薪资期望不太高-有感
    Servlet登陆功能的实现
  • 原文地址:https://www.cnblogs.com/ch2020/p/13060242.html
Copyright © 2020-2023  润新知