Django day06 模版层(二) 过滤器 标签

一: 模板语言之过滤器:
 " | " 前后的区分: 前面的是函数的第一个参数, 后面的是python的一个函数, 冒号后面的是第二个参数
例:  <p>过滤器之默认值:{{ ll | default:'没有值' }}</p>

def index(request):
     name = 'prince'  #字符串
     age = 20  #数字类型
     ll = [233, 290, 'bp', 'dsb']  #列表
     ll2 = []  #空列表
     tu = (1, 2, 3)  #元组
     dic = {'name': 'prince', 'age': 20, 'll': [1, 2, 3, 4]}
                
  # 在模板上相当于执行了test函数,打印了return的结果
  def test():
   print('prince')
   return 'bpcsmdj' 
                    
   # 类和对象
   class Person():  # Person 人
          def __init__(self, name, age):
               self.name = name
               self.age = age

       def get_name(self):
               return self.name  # 对象

         @classmethod
         def cls_test(cls):
              return 'cls'

         @classmethod
         def stetic_test(cls):
              return 'stetic'  #

         prince = Person('prince', 20)
         bastard = Person('bastard', 1000)

         Person_list = [prince, bastard]
         Person_dic = {'prince': prince, 'bastard': bastard}
                    
      # return render(request, 'index.html', {'name': name})
      # locals 会把index视图函数内(***全局变量是不可能的***)所有的变量当做参数传到index.html模版里面,打开连接时都能取到
      return render(request, 'index.html', locals())

1. length
  返回值的长度, 他对字符串和列表都起作用,  

 统计字符串长度:{{ name | length }}

 

 

 

二: 模板之标签

posted @ 2018-11-09 21:31  温暖你的心  阅读(137)  评论(0编辑  收藏  举报