I have a list of records from a database associated with a production order in the following format:
7|P50001929|2024-05-06|23|15|15|0|6-7|000184351|123.0|testing a test remark
8|P50001929|2024-05-06|12345|90|90|0|10-12|000184351|43.0|please work
9|P50001929|2024-05-06|19|234|89084|809|089|000184351|0.0|88
I am passing this to a file called results.html that I want to print each record as a row on the page.
Below is the function that calls a display_results() function which renders results.html:
@app.route('/save_production_data', methods=['POST'])
@login_required
def save_production_data():
print("NOW INSIDE SAVE PRODUCTION DATA FUNCTION")
query_results_str = session.get('query_results', '[]') # Default to empty list if not found
query_results = json.loads(query_results_str)
pdno = query_results[0].get("pdno")
# Get data from the POST request
date_str = request.form.get("date")
print("Date string entered:", date_str)
clock_number = request.form.get("clock_number")
print("Clock number entered:", clock_number)
total = int(request.form.get("total", 0))
print("Total entered:", total)
acceptable = int(request.form.get("acceptable", 0))
print("Acceptable entered:", acceptable)
scrap = int(request.form.get("scrap", 0))
print("Scrap entered:", scrap)
box_reel = request.form.get("box_reel")
print("Box reel entered:", box_reel)
lot_number = request.form.get("lot_number")
print("Lot number entered:", lot_number)
weight = float(request.form.get("weight", 0))
print("Weight entered:", weight)
remarks = request.form.get("remarks")
print("Remarks entered:", remarks)
# Convert the date string to a Python date object
date = datetime.strptime(date_str, "%Y-%m-%d").date()
# Create a new entry regardless of existing data
new_entry = ProductionData(
pdno=pdno,
date=date,
clock_number=clock_number,
total=total,
acceptable=acceptable,
scrap=scrap,
box_reel=box_reel,
lot_number=lot_number,
weight=weight,
remarks=remarks,
)
# Add the new entry to the database
db.session.add(new_entry)
db.session.commit()
print("Production data saved successfully!", "success")
print("Pdno being passed to get_production_data: ", pdno)
print("All production order data currently: ", get_production_data(pdno))
for production_data in get_production_data(pdno):
print("Production Data:")
print(" Date:", production_data.date)
print(" Clock Number:", production_data.clock_number)
print(" Total:", production_data.total)
print(" Acceptable:", production_data.acceptable)
print(" Scrap:", production_data.scrap)
print(" Box/Reel:", production_data.box_reel)
print(" Lot Number:", production_data.lot_number)
print(" Weight:", production_data.weight)
print(" Remarks:", production_data.remarks)
#print("Production data type:", type(production_data))
# Redirect or return with updated data
return display_results(query_results, get_production_data(pdno))
The above code prints the following:
Production Data:
Date: 2024-05-06
Clock Number: 23
Total: 15
Acceptable: 15
Scrap: 0
Box/Reel: 6-7
Lot Number: 000184351
Weight: 123.0
Remarks: testing a test remark
Production Data:
Date: 2024-05-06
Clock Number: 12345
Total: 90
Acceptable: 90
Scrap: 0
Box/Reel: 10-12
Lot Number: 000184351
Weight: 43.0
Remarks: please work
Production Data:
Date: 2024-05-06
Clock Number: 19
Total: 234
Acceptable: 89084
Scrap: 809
Box/Reel: 089
Lot Number: 000184351
Weight: 0.0
Remarks: 88
Here is the display_results() function that renders the html page:
def display_results(query_results, production_data=None):
"""
Display results page with given query results and production data.
"""
print("Display results just called")
print("Passed variable query_results:", query_results)
if production_data:
print("Production data type:", type(production_data))
print("Production data provided:", production_data)
for production_data in production_data:
print("Production Data:")
print(" Date:", production_data.date)
print(" Clock Number:", production_data.clock_number)
print(" Total:", production_data.total)
print(" Acceptable:", production_data.acceptable)
print(" Scrap:", production_data.scrap)
print(" Box/Reel:", production_data.box_reel)
print(" Lot Number:", production_data.lot_number)
print(" Weight:", production_data.weight)
print(" Remarks:", production_data.remarks)
else:
print("No production data provided")
if not isinstance(production_data, list):
print("Production data is not a list, converting to list")
production_data = [production_data]
# Render the results template with both query_results and production_data
return render_template(
'results.html',
query_results=query_results,
production_data=production_data,
)
I am verifying that it is printing everything as expected.
Here is the problem area in results.html:
<!-- Table to display existing production data -->
<h2>Previous Entries</h2>
<table>
<thead>
<tr>
<th>Date</th>
<th>Clock #</th>
<th>Total</th>
<th>Acceptable</th>
<th>Scrap</th>
<th>Balance</th>
<th>Box/Reel</th>
<th>Lot Number</th>
<th>Weight</th>
<th>Remarks</th>
</tr>
</thead>
{% if production_data and production_data|length > 0 %}
{% for data in production_data %}
<tr>
<td>{{ data.date }}</td> <!-- Date -->
<td>{{ data.clock_number }}</td> <!-- Clock # -->
<td>{{ data.total }}</td> <!-- Total -->
<td>{{ data.acceptable }}</td> <!-- Acceptable -->
<td>{{ data.scrap }}</td> <!-- Scrap -->
<td>{{ (data.total or 0) - (data.acceptable or 0) }}</td> <!-- Balance -->
<td>{{ data.box_reel }}</td> <!-- Box/Reel -->
<td>{{ data.lot_number }}</td> <!-- Lot Number -->
<td>{{ data.weight }}</td> <!-- Weight -->
<td>{{ data.remarks }}</td> <!-- Remarks -->
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="11">No previous data found for this production order.</td>
</tr>
{% endif %}
</table>
</body>
For some reason, this html section is only printing the last record added to the database. I want it to be printing each one in a row format. Am I missing something here that may be causing this?