Home > Back-end >  (Flask) Why doesn't the Jinja block in my HTML get all the table entries passed from a SQLAlche
(Flask) Why doesn't the Jinja block in my HTML get all the table entries passed from a SQLAlche

Time:05-26

I'm trying to send table entries to display on HTML using Jinja through a returned SQLAlchemy query result from a route function with a specific constraint (second paragraph) but there's only one entry that makes it through to the Jinja code.

The goal here is to display a given product's information along with the orders it's included in (even if there are other products in that order besides the given product).

In practice, all of this information is properly displayed in HTML tables but I removed their code for ease of reading.

@app.route('/<int:product_id>/product_details', methods=['GET'])
def product_details(product_id):
    order_list = Order.query.all()
    product = Products.query.filter_by(product_id=product_id).first()
    for order_to_process in order_list:
        product_list = order_to_process.product_list.split("\n")
        for prod in product_list [:-1]:
            prod_info = prod.split(":")
            if product_id == int(prod_info[0]):
                orders = Order.query.filter_by(product_list=order_to_process.product_list)
    if order_list:
        return render_template('/product_details.html', product=product, orders=orders)
    else:
        return render_template('/product_details.html', product=product)
{% for order in orders %}
   {{order.order_id}}
   {{order.client_id}}
   {{order.order_date}}
   {{order.order_address}}
   {% set product_list = order.product_list.split("\n") %}
   {% for prod in product_list[:-1] %}
      {% set prod_info = prod.split(":") %}
      {{prod_info[0]}}
      {{prod_info[1]}}
      {{prod_info[2]}}
   {% endfor %}
   {{order.total_price}}
   {{order.paiment_method}}
   {{order.status}}
{% endfor %}

Product table model:

Product
   product_id: int
   product_name: string
   product_description: string
   product_price: float
   product_quantity: int
   product_category: string
   product_image_url: string

Order table model:

Order
   order_id: int
   client_id: int
   order_date: string
   order_address: string
   product_list: string
   total_price: float
   paiment_method: string
   status: string

"product_list" is formatted as so:

"product_id:product_name:quantity\n"

"quantity" is calculated and is not the same as the one present in the Product table

EDIT: Corrected typos: "liste_prods" to "product_list" (variables and stuff were originaly named in French and I translated it for this question but forgot that one)

CodePudding user response:

In each loop cycle you are overwriting the previous orders variable. You need to add the query results to a list.

@app.route('/<int:product_id>/product_details', methods=['GET'])
def product_details(product_id):
    orders = []
    order_list = Order.query.all()
    product = Products.query.filter_by(product_id=product_id).first()
    for order_to_process in order_list:
        product_list = order_to_process.product_list.split("\n")
        for prod in liste_prods[:-1]:
            prod_info = prod.split(":")
            if product_id == int(prod_info[0]):
                orders.extend(Order.query.filter_by(liste_product=order_to_process.product_list))
    if order_list:
        return render_template('/product_details.html', product=product, orders=orders)
    else:
        return render_template('/product_details.html', product=product)

CodePudding user response:

where does 'liste_prods' come from? maybe you can try this:

product = Products.query.get(product_id)
return render_template('/product_details.html', product=product)

use {{product.orders}} in orders html

  • Related