条件:urls.py文件中配置好url的访问路径、models.py文件中有Business表。
在views.py文件中实现的三种方式:
from app01 improt models def business(request): 1. v1=models.Business.objects.all() #对象型 [obj{id,caption,code},obj{id,caption,code}...] 2. v2=models.Business.objects.all().values('id','caption') #字典型 [{'id':1,'caption':'yuweibu'},....] 3. v3=models.Business.objects.all().values_list('id','caption') #元祖型 [(1,yunweibu),(2,kaifubu)] return render(request,'business.html',{'v1':v1,'v2':v2,'v3':v3})
在页面渲染:
在business.html文件中:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <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>
跨表.(点)和__(双下划线)的应用: