views.py
def business(request): v1 = models.Business.objects.all() #queryset
#返回对象,类似一个列表 v2 = models.Business.objects.all().values('id','caption') #例如:select id,caption from business #返回的是字典[{}] v3 = models.Business.objects.all().values_list('id', 'caption') # 例如:select id,caption from business #返回的是元组[()] #查询也可以使用values 和 values_list方法 # v4 = models.Business.objects.values('id','caption') # v4 = models.Business.objects.filter(id=1).first() # print(v4) return render(request,'business.html',{'v1':v1,'v2':v2,'v3':v3})
xxx.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>业务线列表(对象)</h1> <ul> {% for row in v1 %} <li>{{ row.id }}-{{ row.caption }}-{{ row.code }}</li> {% endfor %} </ul> <h1>业务线列表(字典)</h1> <ul> {% for row in v2 %} <li>{{ row.id }}-{{ row.caption }}</li> {% endfor %} </ul> <h1>业务线列表(元组)</h1> {# 通过下标获取值#} <ul> {% for row in v3 %} <li>{{ row.0 }}-{{ row.1 }}</li> {% endfor %} </ul> </body> </html>