使用 Flask/Python 重定向到 URL 时缺少参数
我在从表单向 Flask 路由传递参数时遇到问题。我是 Flask 的新手,所以如果这很简单,我很抱歉。我整个早上都在重新做和重新编码,但毫无结果。我正在尝试返回一个 HTML 页面,其中包含表单中的用户输入值。我已经成功地使用表单(硬币)中的一个字段完成了此操作,但是当我以完全相同的方式添加第二个变量时,我不断收到“缺少 1 个必需的位置参数”错误。
下面的 Flask 代码
@app.route('/addcoin/', methods=['POST', 'GET'])
def addcoin():
if request.method == "POST":
coin = request.form['Symbol']
high_price = request.form['High Price']
api = request.form['API']
channel = request.form['Channel']
# return f"<h1>{coin}{high_price}{api}{channel}</h1>"
print('High Price is ' + high_price)
print(coin)
print(type(high_price))
limit = float(high_price)
print(limit)
return redirect(url_for('coin_added', coin_added=coin, limit=limit))
else:
return render_template('addcoin.html')
@app.route('/new-coin/<coin_added>')
def coin_added(coin_added, limit):
return render_template('new_coin.html', coin=coin_added, high_price=limit)
我正在尝试呈现以下 HTML 模板:
{% extends "index.html" %}
{% block content %}
<h1>{{ coin }} has bee added!</h1>
<h1>{{ high_price }} is the cutoff price!</h1>
{% endblock %}
仅在 '/new 的 coin_add() 函数中使用“coin”参数时没有任何问题-coin/
如果我从 coin_added 函数中删除“limit”参数,一切都会正常。我很困惑为什么它说“限制”参数丢失,当它在上面传递时。
错误消息
TypeError: coin_added() missing 1 required positional argument: 'limit'
I am having trouble with passing arguments to a Flask route from a Form. I am new to Flask so my apologies if this is something simple. I have been re-doing and re-coding things all morning but to no avail. I am trying to return an HTML page with the user input values from a form. I have done it successfully with one field form the form (coin), but when I add a second variable the exact same way I keep getting the "missing 1 required positional argument" error.
Flask Code Below
@app.route('/addcoin/', methods=['POST', 'GET'])
def addcoin():
if request.method == "POST":
coin = request.form['Symbol']
high_price = request.form['High Price']
api = request.form['API']
channel = request.form['Channel']
# return f"<h1>{coin}{high_price}{api}{channel}</h1>"
print('High Price is ' + high_price)
print(coin)
print(type(high_price))
limit = float(high_price)
print(limit)
return redirect(url_for('coin_added', coin_added=coin, limit=limit))
else:
return render_template('addcoin.html')
@app.route('/new-coin/<coin_added>')
def coin_added(coin_added, limit):
return render_template('new_coin.html', coin=coin_added, high_price=limit)
I am trying to render the below HTML template:
{% extends "index.html" %}
{% block content %}
<h1>{{ coin }} has bee added!</h1>
<h1>{{ high_price }} is the cutoff price!</h1>
{% endblock %}
I have no issues when only using the "coin" argument within the coin_added() function of the '/new-coin/<coin_added>' route. But when I try and add the limit and high price variable it insists that the argument is missing. I am even printing out the "limit" variable to console to see if it exists and it prints out successfully before the redirect line. I am not sure why this is not getting passed to the coin_added route as limit.
If I remove the "limit" argument from the coin_added function everything works fine. I am very confused as to why it is saying the "limit" argument is missing, when it is getting passed in right above this.
Error Message
TypeError: coin_added() missing 1 required positional argument: 'limit'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的路线缺少限制变量。它应该是这样的 有关
如何使用多个参数的更多信息此处。
I think your route is missing the limit variable. It should be something like this
More information on how to have multiple parameters here.