我有一个数据库表,允许您输入有关特定人员的详细信息。然后,我如何列出该表的所有条目以显示添加到数据库的所有人。
urls.py
url(r'^accounts/loggedin/locations/all/$', 'assessments.views.locations'),
url(r'^accounts/loggedin/locations/get/(?P<location_id>\d+)$', 'assessments.views.location'),
url(r'^accounts/loggedin/locations/create/$', 'assessments.views.create'),
models.py
class People(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
view.py
def people(request):
return render_to_response('dashboard/people.html', {'people': People.objects.all})
people.html
<body>
<h1>People</h1>
{% for p in people %}
<h1><a href="/people/get/{{ people.id }}/">{{ people.first_name }}</a></h1>
{% endfor %}
</body>
</html>
答案 0 :(得分:2)
两个问题:
您应致电all()
以获取结果,请注意()
:
People.objects.all()
,您应该使用{{ p.first_name }}
而不是{{ people.first_name }}
,因为您正在迭代people
QuerySet
变量 - 一个对象列表,基本上