2

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>
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Chris Meek
  • 1,473
  • 5
  • 20
  • 31
  • 1
    What is your output right now? Looks like you should be using `{{ p.first_name }}` inside your for. This way, if your data is correct, it will iterate over all `people` and print an `

    ` for each person, with the correct name.

    – Lovato Sep 08 '14 at 13:09
  • what is the problem? – Hasan Ramezani Sep 08 '14 at 13:10
  • I have tried what is suggested but i am getting no output on the screen. Its just bank – Chris Meek Sep 08 '14 at 13:40

2 Answers2

4

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

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

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.

Soulduck
  • 569
  • 1
  • 6
  • 17