0

I am successfully getting the symbol from the button (as see in image) but I am not sure how load it into the text field as value to be submitted. Please help....enter image description here

    @app.route("/buy", methods=["GET", "POST"])
    @login_required
    def buy():
        """Buy shares of stock"""
        if request.method == "POST":
    
    
    
            #gets the symbol from user
            if not request.form.get("symbol"):
                return apology("must enter stock symbol", 403)
    
            #gets the symbol from user
            if not request.form.get("shares"):
                return apology("must enter the amount of shares", 403)
    
            #ensures value entered is integer
            elif not request.form.get("shares").isdigit():
                return apology("invalid share amount", 403)
    
            stock = lookup(request.form.get("symbol").upper())
    
            #passes user input to lookup function then pass it to holder
    
    
            shares = int(request.form.get("shares"))
    
            #checks for invalid stock
            if stock == None:
                return apology("Invalid Stock Symbol", 403)
    
            row = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
    
            up_cash = row[0]["cash"] - (shares * stock["price"])
    
    
            if up_cash < 0:
                return apology("Insufficient Funds", 403)
    
            #line used to update cash amount after purchaing stock
            db.execute("UPDATE users SET cash = :up_cash WHERE id = :id", up_cash =  up_cash, id = session["user_id"])
    
            #line used to insert details amount the purchase into the transactions table
            db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES (:user_id, :symbol, :shares, :price)", user_id= session["user_id"], symbol = stock["symbol"], shares = shares, price = stock["price"])
    
            flash("Transaction Successful")
    
            #redirect user to quoted page as well as passes data from query to quoted page
            return redirect("/")
    
    
    
        else:
            symb = request.form.get('symbol_buy')
            return render_template("buy.html", stocks = sym)
    {% extends "layout.html" %}
    
    {% block title %}
        Buy
    {% endblock %}
    
    {% block main %}
        <form action= "/buy" method = "post">
            <div class= "form-group">
                <input autocomplete = "off" autofocus class= "form-control" name= "symbol" placeholder ="Symbol" type= "text">
            </div>
             <div class= "form-group">
                <input autocomplete = "off" class= "form-control" name= "shares" placeholder ="Shares" min = "1" type= "number" >
            </div>
            <button class="btn btn-primary" type="submit">Buy</button>
        </form>
    {% endblock %}
1

Add value = "{{ stocks }}" to the symbol input element in buy.html. There are some problems in the GET branch of the buy route:

  • To retrieve values from a GET request, use request.args instead of request.form (i.e. request.args.get('symbol_buy'))
  • Beware discrepancy symb sym

If request.args.get('symbol_buy') is None, you may want to send the empty string ("") as the value.

1
  • Thank you so much!! That fixed everything. Now I am proudly say I am finished. May 16 at 1:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .