Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, display an HTML table from a list of dictionary items

I'm writing an order queue system using django and I've been able to figure out how to get input from HTML forms and store it into an sqlite database using models and forms. To read data from the database into a list I do something like this

queueList = Order.objects.filter(orderDate__isnull=True)

(html output of {{queueList}})

Where Order is a model with a number of attributes. The data returned looks something like this.

[<Order: Name: Bob, Catalog #: 32, Vendor: vendor1, Site: Site1, Entered: 2011-06-06,     Ordered: None, Received: None, Ordered By: orderee1>, <Order: Name: Jeff, Catalog #: 333, Vendor: vendor2, Site: site2, Entered: 2011-06-06, Ordered: None, Received: None, Ordered By: orderee2>] 

It feels like there would be something simple built into django to format this as an HTML table, kind of like form.as_table. Is there any easy way to do this? I need to display 3 tables, first prints out every dictionary item that has no orderDate, the second is every item that as orderDate but no receivedDate, and the third is a table that has all receivedDates. Am I going to have to actually chop up all this data in order to construct a table? I found some code online of someone who tried something similar but it was throwing exceptions and was too complex for me to fix.

like image 213
John Lotacs Avatar asked Dec 06 '22 21:12

John Lotacs


1 Answers

This sounds like something that can be easily solved using the Django templating language.

So, in your html template file, you could have something like:

<table summary="no_orderDate">
<tr><th>Column 1</th><th>Column 2</th>
{% for queue in queueList %}
    {% if not queue.orderDate %}
        <tr><td>{{ queue.name }}</td><td>{{ queue.catalog_num }}</td></tr>
    {% endif %}
{% endfor %}
</table>

You might need to check some of the syntax, but you get the idea.

like image 135
Steve Walsh Avatar answered May 22 '23 08:05

Steve Walsh