如何解决CS50财务购买实施
我在尝试购买股票时收到错误消息“ 500 Internal Server Error”。你能告诉我我在想什么吗? application.py代码为:
@app.route("/buy",methods=["GET","POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":
quote = lookup(request.form.get("symbol"))
#Check if valid symbol
if quote == None:
return apology("Must provide a valid symbol",400)
#Check if shares are an integer
try:
shares = int(request.form.get("shares"))
except:
return apology("share number must be a positive integer",400)
#Check if shares entered >0
if shares <= 0:
return apology("shares must be positive",400)
#Query database for username
rows = db.execute("SELECT cash FROM users WHERE id = :user_id",user_id = session["user_id"])
#Check the cash balance
cash_remaining = rows[0]["cash"]
price_per_share = quote["price"]
#Calculate cash required to buy shares
total_price = price_per_share * shares
#Check if user has enough money
if total_price > cash_remaining:
return apology("You don't have enough cash")
#Insert new cash into users database
db.execute("UPDATE users SET cash = cash - :total_price WHERE id = :user_id",total_price = total_price,user_id=session["user_id"])
#Insert transaction into transactions database
db.execute("INSERT INTO transactions (user_id,symbol,shares,price_per_share,total_price,) VALUES(:user_id,:symbol,:shares,:price_per_share,:total_price)",user_id = session["user_id"],symbol = request.form.get("symbol"),shares = shares,price_per_share = price_per_share,total_price = total_price)
flash("Bought!")
return redirect(url_for("index"))
else:
return render_template("buy.html")
与application.py相对应的buy.html代码是。由于某种原因,我无法使其正常工作。任何提示将不胜感激。
{% 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" autofocus class="form-control" name="shares" placeholder="Shares" type="number">
</div>
<button class="btn btn-primary" type="submit">Buy</button>
</form>
{% endblock %}
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。