5

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.

2 Answers 2

19

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.

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

4 Comments

Thanks, I have it working fine now. One issue, I want to ovrride my CSS for a table later in the page(django form), I use <table style="padding:0px;"> but it isn't overriding my external CSS file, how do I get this to work?
I would have to see the html code. You may be overriding css for the table tag but not for the row tag or something like that.
You're right, I need to override for the td tag but I'm using django's {{ form.as_table }} to print the table with I want with no padding, so I can't directly put a <td style="padding:0px;">, is there a way to do it in my python file?
I've never used form.as_table but what you probably want to do is use a class to describe how your table should look like. Not sure if there is a way to specify a CSS class for form.as_table. It's generally not the best idea to put style tags in your HTML.
3

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>

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.