0

I've got a model class, House, with several columns. I want to get all the entries to this table, with all the columns, and display these on in a table in a template.

Firstly, how do I pull all the information that I need out of the database (and into a 2d list?), and what tag could I use to access specific data in the table?

babbaggeii
  • 7,577
  • 20
  • 64
  • 118

1 Answers1

1

it's as easy as stated here

so, practically:

all_houses = Houses.objects.all()

will give you all the entries in your database.

from within a view, pass that variable to the template context then, in the template:

{% for house in all_houses %}
    {{ house.<column_name> }}
{% endfor %}

let me explain this bit of code: once you pass all your entries to the template, you can loop them with {% for %}

{{ house. }} means that you can extract the value you need from the column you need ( column_name ) and place it wherever you want (from whitin the for loop, obviously) so you can have (for example) {{ house.price }}, {{ house.bathrooms }} and so on, for each entry you have in your "all_entries"

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
  • Ah right, thanks. That does make sense. I suppose I was wondering what sort of data was held in all_houses, but it's an object that holds all the data and you interrogate that on the template. Cheers. – babbaggeii Oct 18 '12 at 14:57
  • yes, give a big look at the link i provided, django is way easier than it seems at the beginning (also, can you accept the answer after you tested if it's correct for you? cheers! :) ) – Samuele Mattiuzzo Oct 18 '12 at 14:59