我在將引數從表單傳遞到 Flask 路由時遇到問題。我是 Flask 的新手,所以如果這很簡單,我深表歉意。我整個早上都在重新做和重新編碼,但無濟于事。我正在嘗試使用表單中的用戶輸入值回傳一個 HTML 頁面。我已經成功地使用表單(硬幣)的一個欄位完成了它,但是當我以完全相同的方式添加第二個變數時,我不斷收到“缺少 1 個必需的位置引數”錯誤。
下面的燒瓶代碼
@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 %}
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'
uj5u.com熱心網友回復:
我認為您的路線缺少限制變數。它應該是這樣的
@app.route('/new-coin/<coin_added>/<limit>')
有關如何在此處設定多個引數的更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433480.html
