【发布时间】:2020-02-17 20:46:22
【问题描述】:
我正在解决 CS50 的财务问题集。 (https://cs50.harvard.edu/x/2020/tracks/web/finance/)
在此任务中,用户应该能够在 html 页面上提交表单以请求公司股票的当前价格。
以下是我迄今为止为报价路线所做的(使用 python 和烧瓶):
@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
if request.method == "POST":
quote = lookup(request.form.get("requested_share"))
if quote == None:
return apology("No share found for this symbol")
return render_template("quoted.html", quote=quote)
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("quote.html")
这是我为“引用”html 页面编写的内容:
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/quote" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="requested_share" placeholder="e.g. TSLA" type="text" />
</div>
<button class="btn btn-primary" type="submit">Request Quote</button>
</form>
{% endblock %}
最后,这是我为“引用”的 html 页面(如果他们的报价请求成功,用户应该被带到该页面)获得的内容:
{% extends "layout.html" %}
{% block title %}
Quoted
{% endblock %}
{% block main %}
<p>A share of {{ quote["name"] }} costs {{ quote["price"] }}</p>
{% endblock %}
但是,当我尝试它时,它似乎表现得好像用户没有输入任何内容。它没有转到quoted.html 页面,而是从这部分代码转到道歉页面:
if quote == None:
return apology("No share found for this symbol")
有人可以帮忙吗?我已经盯着它看了好多年,仍然看不出它哪里出了问题。我认为这可能与 API 密钥有关,尽管我已按照页面上的说明进行操作
【问题讨论】:
-
一个快速的解释是
API_KEYfor IEX 在flask run之前的环境中不会被导出。并密切关注规范,即Require that a user input a stock’s symbol, implemented as a text field whose name is symbol