Why isn't the flask implementation of the following working? please fix: {% extends…
- User
Why isn't the flask implementation of the following working? please fix: {% extends "layout.html" %}
{% block title %} History and Herstory {% endblock %}
{% block main %}
</thead> <tbody> {% for transaction in purchases %} <tr> <td class="text-start">{{ purchase.symbol }}</td> <td class="text-end">{{ purchase.shares }}</td> <td class="text-end">{{ purchase.price | usd }}</td> <td class="text-end">{{ purchase.timestamp }}</td> </tr> {% endfor %} </tbody> </table>{% endblock %}
here's app.py: @app.route("/history", methods=["GET"]) @login_required def history(): purchases = db.execute(""" SELECT symbol, shares, price, timestamp FROM purchases WHERE user_id = :id; """, id=session['user_id'])
return render_template("index.html", symbol=symbol, shares=shares, purchases=purchases, timestamp=timestamp)And finance.db sqlite3 .schema is: finance.db sqlite3 .schema is: CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00); CREATE TABLE sqlite_sequence(name,seq); CREATE UNIQUE INDEX username ON users (username); CREATE TABLE purchases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, symbol TEXT NOT NULL, shares INTEGER NOT NULL, price REAL NOT NULL, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users (id));
- Assistant
The problem seems to be in the way you are referencing the purchases within the Flask template and passing the data from the Flask app to the template.
First, inside the for-loop in your Flask template, you are using
purchase(singular) to access the properties of each transaction while iterating. However, you've named the loop variabletransaction. To fix this inconsistency, you should use the same variable name.