python - SQLAlchemy model data isn't rendered in Jinja loop -
i'm trying display menu of items (like lunch menu) can't them show up. added items via flask-admin page , checked database verify items exist know that's not problem. why doesn't menu item data render in loop?
class menuitems(db.model): id = db.column(db.integer(), primary_key=true) name = db.column(db.string(50), index=true, unique=true) price = db.column(db.float(6), unique=false) description = db.column(db.string(140)) @app.route('/menu') def menu(): menu_items = menuitems.query.all() return render_template('menu.html', title="dave's bread - menu", menu_items=menu_items, user=current_user)
{% item in menu_items %} <td> <p><strong>{{ menu_items.name }}</strong></p> <p>{{ menu_items.description }}</p> <p>price: {{ menu_items.price }}</p> </td> {% endfor %}
you're accessing properties on menu_items
inside loop. meant use item
access properties of each item.
flask's jinja environment skips rendering undefined values default. menu_items
doesn't have description
attribute, jinja treats undefined. can make more strict using different undefined type.
from jinja2 import strictundefined app.jinja_env.undefined = strictundefined
Comments
Post a Comment