this is my code
sell1.html
{% for stock in stocks %}
<tr>
<th scope="row" name="stock">{{ stock[0] }}</th>
<td>{{ stock[1] }}</td>
<td>{{ stock[2] }}</td>
<td>{{ stock[3] }}</td>
<td>{{ stock[4] }}</td>
<td><a href="sell1/{{ stock[0] }}" class = "btn btn-default pullright">Sell</a> </td>
</tr>
{% endfor %}
app.py
@app.route("/sell/<string:stock>", methods=["GET", "POST"])
@login_required
def sell1(stock):
"""Sell shares of stock"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# collect relevant informations
amount = int(request.form.get("amount"))
symbol = request.form.get("symbol")
price = lookup(symbol)["price"]
value = round(price * float(amount))
# Update stocks table
amount_before = db.execute("SELECT amount FROM stocks WHERE user_id = :user AND symbol = :symbol",
symbol=symbol, user=session["user_id"])[0]['amount']
amount_after = amount_before - amount
# delete stock from table if we sold every unit we had
if amount_after == 0:
db.execute("DELETE FROM stocks WHERE user_id = :user AND symbol = :symbol",
symbol=symbol, user=session["user_id"])
# stop the transaction if the user does not have enough stocks
elif amount_after < 0:
return apology("That's more than the stocks you own")
# otherwise update with new value
else:
db.execute("UPDATE stocks SET amount = :amount WHERE user_id = :user AND symbol = :symbol",
symbol=symbol, user=session["user_id"], amount=amount_after)
# calculate and update user's cash
cash = db.execute("SELECT cash FROM users WHERE id = :user",
user=session["user_id"])[0]['cash']
cash_after = cash + price * float(amount)
db.execute("UPDATE users SET cash = :cash WHERE id = :user",
cash=cash_after, user=session["user_id"])
# Update history table
db.execute("INSERT INTO transactions(user_id, symbol, amount, value) VALUES (:user, :symbol, :amount, :value)",
user=session["user_id"], symbol=symbol, amount=-amount, value=value)
# Redirect user to home page with success message
flash("Sold!")
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
# Add Symbol
# query database with the transactions history
rows = db.execute("SELECT symbol, amount FROM stocks WHERE (user_id = :user AND symbol = :stock)",
user=session["user_id"], stock=stock)
stocks = {}
for row in rows:
stocks[row['symbol']] = row['amount']
return render_template("sell1.html", stocks=stocks)
and
in index.html:
{% for stock in stocks %}
<tr>
<th scope="row" name="stock">{{ stock[0] }}</th>
<td>{{ stock[1] }}</td>
<td>{{ stock[2] }}</td>
<td>{{ stock[3] }}</td>
<td>{{ stock[4] }}</td>
<td><a href="sell1/{{ stock[0] }}" class = "btn btn-default pullright">Sell</a> </td>
</tr>
index page works fine as it send this Get:
"GET /sell1/AAPL HTTP/1.1" 404 -
But after I have issue as it is a 404 page with this url: http://127.0.0.1:5000/sell1/AAPL
I tried to check sql query and it works fine. this is schema of stocks table: 'stocks' ('id' integer PRIMARY KEY AUTOINCREMENT NOT NULL, 'user_id' integer NOT NULL, 'symbol' char(4) NOT NULL, 'amount' integer NOT NULL);
thanks for help as I m new in flask and python...
thanks to mouse tail, In my route, I forgot sell1
@app.route("/sell1/<string:stock>", methods=["GET", "POST"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.