变量
为了便于演示,一下是views.py的代码
def test(request):
num = [1,2,3,4]
dir = {'lemon':'luouo','banana':'haha','apple':'bubu'}
class Commodity(object):
def __init__(self,name,price):
self.name=name
self.price = price
def feel(self):
return '%s is too expensive' %self.name
house = Commodity('house','One hundred million')
car = Commodity('car','One hundred thousand')
dream=[house,car]
return render(request,'test.html',{'Num':num,'Dir':dir,'Dream':dream})
html中的用法
{{ Num }} #显示[1,2,3,4]
{{ Num.0 }} #取第一个值
{{ Dir.lemon }} #取字典中lemon的值
{{ Dream.0.name }} #取对象name的属性
{{ Dream.0.feel }} #取对象的feel方法的返回值 house is too expensive