Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Markup (HTML, XHTML, XML) _ Query SQLAlchemy objects on HTML page

Posted by: Londy Jan 13 2024, 05:18 AM

In Flask, SQLAlchemy, I created a class, Food

CODE
class Food(db.Model):    
   __tablename__ = "food"
    food_id = db.Column(db.Integer, primary_key=True)
    food_name = db.Column(db.String(20), unique=True, nullable=False)
    food_price = db.Column(db.Numeric(10,2), nullable=False)
    food_type = db.Column(db.String(30), nullable=False)


I queried the data in Flask and render it to an HTML page

CODE
@app.route('/welcome')
def welcome():
    food = db.session.query(Food).all()
    return render_template('welcome.html', food=food)


I want to query the data on the HTML page, i.e have a section to show food_names of food_type 'drinks' and the other section' to show food_names of 'snacks' on the same web page. Therefore I want to send all the data to the html page and filter there. The issue I have is the query on the HTML page. I get the food_names of ALL food_types. Filter is not working.

CODE
<head>
    <script type = "text/javascript">
function(foodQ)
{return food.food_type==’drink’
}
</script>
</head>
<body>
{%for food in food%}
{{food.food_name}}
{%endfor%}



Posted by: Londy Jan 13 2024, 07:18 AM

I corrected the code. It now works.

CODE
{%for food in food%}
                    {% if food.food_type=='drink' %}
                    <a href="java script:window.open('{{url_for('menu', type = food.food_name)}}', 'width=20,height=50', left=400, top =10);">{{food.food_name}}</a> <br>
                    {% endif %}
                    {%endfor%}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)