Python Django 获取表单数据的三种方式

# In views
def
zbsservice(request): #返回一个列表 v1 = models.Business.objects.all() # .value返回一个字典 v2 = models.Business.objects.all().values("id","caption","code") # .values_list 返回一个元组 v3 = models.Business.objects.all().values_list('id','caption') return render(request,'zbsservice.html',{'v1':v1,'v2':v2,'v3':v3})
# In html
<h1>ServiceName:(列表)</h1> {% for item in v1 %} <ul><a>{{ item.id }}</a>-<a>{{ item.caption }}</a>-<a>{{ item.code }}</a></ul> {% endfor %} <h1>ServiceName:(字典)</h1> {% for item in v2 %} <ul><a>{{ item.id }}</a>-<a>{{ item.caption }}</a>-<a>{{ item.code }}</a></ul> {% endfor %} <h1>ServiceName:(元组)</h1> {% for item in v3 %} <ul><a>{{ item.0 }}</a>-<a>{{ item.1 }}</a></ul> {% endfor %}
原文地址:https://www.cnblogs.com/yooma/p/8028909.html