Check my app.py code. For some reason /sell is not properly rendering. I can't see the…
- User
Check my app.py code. For some reason /sell is not properly rendering. I can't see the selector to sell my stocks. Here's my app.py code: import os
from cs50 import SQL from flask import Flask, flash, redirect, render_template, request, session from flask_session import Session from werkzeug.security import check_password_hash, generate_password_hash
from helpers import apology, login_required, lookup, usd
Configure application
app = Flask(name)
Custom filter
app.jinja_env.filters["usd"] = usd
Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" Session(app)
Configure CS50 Library to use SQLite database
db = SQL("sqlite:///finance.db")
@app.after_request def after_request(response): """Ensure responses aren't cached""" response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" response.headers["Expires"] = 0 response.headers["Pragma"] = "no-cache" return response
@app.route("/") @login_required def index(): # Assume db.execute() returns a dict or a list of dicts user_cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session['user_id'])[0]['cash'] purchases = db.execute(""" SELECT symbol, SUM(shares) as shares, AVG(price) as price FROM purchases WHERE user_id = :id GROUP BY symbol HAVING SUM(shares) > 0; """, id=session['user_id']) # Fixed indent of for loop and calculate total for purchase in purchases: purchase['total'] = purchase['shares'] * purchase['price']
# Moved the summation of total_assets out of the for loop total_assets = sum(purchase['total'] for purchase in purchases) + user_cash return render_template("index.html", cash=user_cash, purchases=purchases, total_assets=total_assets)@app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): if request.method == "GET": return render_template("buy.html") elif request.method == "POST": symbol = request.form.get('symbolb').upper() # Get symbol and ensure it's uppercase try: quantity = int(request.form.get('quantity')) except ValueError: return apology("Invalid quantity", 400)
if quantity <= 0: return apology("Quantity must be more than 0", 400) stock = lookup(symbol) if stock is None: return apology("Invalid stock symbol", 400) current_price = stock['price'] user_cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session['user_id'])[0]['cash'] total_cost = current_price * quantity if total_cost > user_cash: return apology("Not enough cash to complete purchase", 403) db.execute("UPDATE users SET cash = cash - :cost WHERE id = :id", cost=total_cost, id=session['user_id']) # Insert the new purchase db.execute("INSERT INTO purchases (user_id, symbol, shares, price) VALUES (:user_id, :symbol, :shares, :price)", user_id=session['user_id'], symbol=symbol, shares=quantity, price=current_price) flash("Purchase successful!") return redirect("/") - Assistant
Your
app.pyandsell.htmlseem to be prepared correctly for handling the stock-selling functionality, and thesell.htmltemplate is correctly set up to display a selector ( element) with stock symbols. - User
- Assistant
Thank you for providing your
layout.html. From what I can see, the layout template looks well-structured and should work correctly with Flask and Bootstrap.In
layout.html, the{% block main %}{% endblock %}is supposed to be overridden by individual templates that extendlayout.html(like yoursell.htmldoes). However, in yoursell.htmlcode, you are actually using{% block body %}instead of{% block main %}.To fix the issue, you should replace
{% block body %}with{% block main %}in yoursell.htmltemplate. Here is the corrected top part ofsell.html:{% extends "layout.html" %} {% block title %} Sell {% endblock %} {% block main %} <div class="mb-3"> <!-- Form contents remain unchanged --> ... {% endblock %}Once you have made this change, your
sell.htmltemplate should correctly replace the block inlayout.htmlas intended, and if there were no other issues, the stock selection dropdown should render properly when you visit the/sellroute in your Flask app.