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 act开发者_高级运维ually 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.
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.
If i understand correctly you are looking to just dump all of the fields out without having to write the output as in ZincX's answer? If so you could do something like the below however, just simply doing it like this means you loose control of the output.
# in models.py Order class
def get_fields(self):
return [(field, field.value_to_string(self)) for field in Order._meta.fields]
# in template
<table>
{% for queue in queueList %}
{% for field, value in queue.get_fields %}
<tr>
<th>{{ field }}</th>
<td>{{ value }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
精彩评论