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>
3
  • 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 <h1> for each person, with the correct name. Commented Sep 8, 2014 at 13:09
  • what is the problem? Commented Sep 8, 2014 at 13:10
  • I have tried what is suggested but i am getting no output on the screen. Its just bank Commented Sep 8, 2014 at 13:40

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.