條件: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>
跨表.(點)和__(雙下划線)的應用:
