5

I want to display a table showing the values of three fields for each of the units in a table. Some assistance with creating the dictionary from the database and passing objects to the template would be greatly appreciated.

@app.route('/')
def index(row):
    unit_count = Units.query.count()
    unit = Units.query.filter_by(id=row).first()
    rows = [] # Unsure how to define rows from Units db table
    return render_template('table_overview.html', title='Overview', rows=rows, unit=unit)
{% for row in rows %}
<tr>
    <td>{{ row.unit.idnum }}</a></td>
    <td>{{ row.unit.product_code }}</td>
    <td bgcolor="{{ row.unit.stat_colour }}">{{ row.unit.unit_status }}</td>
</tr>
{% endfor %}

1 Answer 1

7

First off your view function can't receive the row input you specify. If you're trying to show all rows in the table you can do it like this:

views.py:

@app.route('/')
def index():
    rows = Units.query.all()
    return render_template('table_overview.html',
                            title='Overview',
                            rows=rows)

table_overview.html (template)

    {% for row in rows %}
    <tr>
        <td>{{ row.idnum }}</a></td>
        <td>{{ row.product_code }}</td>
        <td bgcolor="{{ row.stat_colour }}">{{ row.unit_status }}</td>
    </tr>
    {% endfor %}
Sign up to request clarification or add additional context in comments.

2 Comments

That did it! Far simpler than I'd expected. Thanks very much for helping me, I really appreciate it!
Thanks! It sure beats generating html in pure Python scripts ;) Flask has answered my prayers.

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.