Query SQLAlchemy objects on HTML page, Selecting all food types and separating them on HTML page |
Query SQLAlchemy objects on HTML page, Selecting all food types and separating them on HTML page |
Londy |
Jan 13 2024, 05:18 AM
Post
#1
|
Group: Members Posts: 6 Joined: 19-December 23 Member No.: 29,102 |
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%} |
Londy |
Jan 13 2024, 07:18 AM
Post
#2
|
Group: Members Posts: 6 Joined: 19-December 23 Member No.: 29,102 |
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%} |
Lo-Fi Version | Time is now: 21st May 2024 - 10:31 AM |