[英]how to add variable in a action post Flask and Python
我仍然在我的 flask 项目中继续,我的问题是当我点击出售按钮时,我有一个错误。 我认为是由于发布操作发送无效的 url。 它发送 POST /sell1 HTTP/1.1
我认为它应该发送 POST /sell1/AAPL HTTP/1.1 (例如,AAPL 是我的代码中变量符号的值)感谢您的帮助。
我得到的错误是:找不到 404
我的代码:
出售1.html:
{% block main %}
<form action="/sell1/" method="post">
<div class="form-group">
<text id="stock" name="symbol">
{{ stocks }}
</text>
, owned
<text id="nbr" name="amount">
{{ nbr }}
</text>
</div>
<div class="form-group">
<input min="1" autocomplete="off" autofocus class="form-control" name="amount" placeholder="Amount" type="number">
</div>
<button id="submit" class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}
应用程序.py
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)
Nbr = rows[0]['amount']
return render_template("sell1.html", stocks=stock, nbr=Nbr)
我找到了 javascript 的解决方案:
<form id="actionform" method="post" onsubmit="get_stock_quote()">
<div class="form-group">
<input type="text" id="stock" name="symbol" value="{{ stocks }}">
, owned
<text id="nbr" name="amountbefore">
{{ nbr }}
</text>
</div>
<div class="form-group">
<input min="1" autocomplete="off" autofocus class="form-control" name="amount" placeholder="Amount" type="number">
</div>
<button id="submit" class="btn btn-primary" type="submit">Sell</button>
</form>
<script language="javascript" type="text/javascript">
function get_stock_quote()
{
var form = document.getElementById('actionform')
var symbol = document.getElementById('symbol').value
var action_src = "/sell1/" + symbol
form.action = action_src;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.