CS50 金融 - 卖出(遍历下拉菜单)
CS50 Finance - Sell (iterate over dropdown menu)
我正在网页上实现下拉菜单。在我的控制器代码中,我从 sqlite 数据库中获取数据并将其存储在一个名为“portfolio”的变量中。然后我用 Jinja 在我的 html 页面上迭代它。我在另一页上成功地遵循了同样的过程。唯一的区别是该信息显示在 table 而不是下拉菜单中。我怀疑有关将项目存储在“选项值”标签中的问题给我造成了问题。
任何人都可以帮助指出正确的方向吗?谢谢
My problem: no stocks displaying in dropdown menu
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""
# If a form is submitted
if request.method == "POST":
symbol = request.form.get("symbol")
shares_to_sell = int(request.form.get("shares"))
# Access user's stock info
portfolio = db.execute("SELECT symbol, SUM(shares) AS shares FROM purchases WHERE users_id = ? GROUP BY symbol", session["user_id"])
for stock in portfolio:
folio_symbol = stock["symbol"]
shares_owned = int(stock["shares"])
# Ensure stock is selected
if not symbol:
return apology("Must select a stock to sell", 403)
# Ensure user owns that stock
if symbol != folio_symbol:
return apology("Can only sell stocks you own", 403)
# If no shares selected
if shares_to_sell < 1:
return apology("Must select a number of shares to sell", 403)
# If user does not own enough shares
if shares_owned < shares_to_sell:
return apology("User does not own enough shares to sell", 403)
# Subtract shares sold from purchases table
db.execute("UPDATE purchases SET shares = ? WHERE users_id = ?", shares_owned-shares_to_sell, session["user_id"])
# Adjust cash in users table
users = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])
cash = users[0]["cash"]
stock_info = lookup(symbol)
sell_amt = shares_to_sell*stock_info["price"]
db.execute("UPDATE users SET cash = ? WHERE id = ?", cash+sell_amt, session["user_id"])
flash("Sold!")
return redirect("/", portfolio=portfolio, stock=stock)
else:
return render_template("sell.html")
HTML 页数:
{% extends "layout.html" %}
{% block title %}
Sell Stocks
{% endblock %}
{% block main %}
<h2>Sell Stocks</h2>
<br>
<p>Offload your shares below.</p>
<br>
<form action="/sell" method="post">
<div class="form-group">
<select class="form-control" name="symbol">
<option disabled selected value>Stock</option>
{% for stock in portfolio %}
<option value="{{ stock.folio_symbol }}">{{ stock.folio_symbol }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<input class="form-control" name="shares" placeholder="Shares" type="text">
</div>
<button class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}
已解决:我忘记在 GET 条件下获取我的数据库信息。因此,下拉列表未填充,因为在该条件下没有可用数据来填充它。
我正在网页上实现下拉菜单。在我的控制器代码中,我从 sqlite 数据库中获取数据并将其存储在一个名为“portfolio”的变量中。然后我用 Jinja 在我的 html 页面上迭代它。我在另一页上成功地遵循了同样的过程。唯一的区别是该信息显示在 table 而不是下拉菜单中。我怀疑有关将项目存储在“选项值”标签中的问题给我造成了问题。
任何人都可以帮助指出正确的方向吗?谢谢
My problem: no stocks displaying in dropdown menu
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""
# If a form is submitted
if request.method == "POST":
symbol = request.form.get("symbol")
shares_to_sell = int(request.form.get("shares"))
# Access user's stock info
portfolio = db.execute("SELECT symbol, SUM(shares) AS shares FROM purchases WHERE users_id = ? GROUP BY symbol", session["user_id"])
for stock in portfolio:
folio_symbol = stock["symbol"]
shares_owned = int(stock["shares"])
# Ensure stock is selected
if not symbol:
return apology("Must select a stock to sell", 403)
# Ensure user owns that stock
if symbol != folio_symbol:
return apology("Can only sell stocks you own", 403)
# If no shares selected
if shares_to_sell < 1:
return apology("Must select a number of shares to sell", 403)
# If user does not own enough shares
if shares_owned < shares_to_sell:
return apology("User does not own enough shares to sell", 403)
# Subtract shares sold from purchases table
db.execute("UPDATE purchases SET shares = ? WHERE users_id = ?", shares_owned-shares_to_sell, session["user_id"])
# Adjust cash in users table
users = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])
cash = users[0]["cash"]
stock_info = lookup(symbol)
sell_amt = shares_to_sell*stock_info["price"]
db.execute("UPDATE users SET cash = ? WHERE id = ?", cash+sell_amt, session["user_id"])
flash("Sold!")
return redirect("/", portfolio=portfolio, stock=stock)
else:
return render_template("sell.html")
HTML 页数:
{% extends "layout.html" %}
{% block title %}
Sell Stocks
{% endblock %}
{% block main %}
<h2>Sell Stocks</h2>
<br>
<p>Offload your shares below.</p>
<br>
<form action="/sell" method="post">
<div class="form-group">
<select class="form-control" name="symbol">
<option disabled selected value>Stock</option>
{% for stock in portfolio %}
<option value="{{ stock.folio_symbol }}">{{ stock.folio_symbol }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<input class="form-control" name="shares" placeholder="Shares" type="text">
</div>
<button class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}
已解决:我忘记在 GET 条件下获取我的数据库信息。因此,下拉列表未填充,因为在该条件下没有可用数据来填充它。