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....
@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 %}