Chris Meek
Chris Meek

Reputation: 1523

Displaying a list of table entries in Django

I have a database table that allows you to enter details about a particular person. How can i then list all the entries of that table to show all the people added to the database.

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>

Upvotes: 2

Views: 3415

Answers (2)

Soulduck
Soulduck

Reputation: 615

it is same question with this link


If you had the Question model below,

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField(verbose_name='date published')

Through this code, you can see all entries (or fields, features).

fields = Question._meta.get_fields()

thanks.

Upvotes: 0

alecxe
alecxe

Reputation: 473763

Two problems:

  • you should call all() to get the results, note the ():

    People.objects.all()
    
  • in the template, you should use {{ p.first_name }} instead of {{ people.first_name }} since you are iterating over people variable which is a QuerySet - a list of objects, basically

Upvotes: 4

Related Questions