我有一個簡單的表單,允許用戶提交ID的兩種型別:一種他們自己的輸入和一個應該根據自己給表單輸入選擇提交的值。
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
heroes = requests.get(f"{BASE_URL}/heroes").json()
return render_template("index.html", heroes=heroes)
else:
steam32_id = request.form.get('steam32_id')
hero_id = request.form.get('hero_id')
return {"steam32_id": steam32_id, "hero_id": hero_id}
<form method="post" action="{{ url_for('index') }}">
<label for="steam32_id">Steam ID</label>
<input type="text" id="steam32_id" name="steam32_id"> <br>
<label for="hero_list">Select Hero</label>
<select id="hero_list" name="hero_list">
{% for hero in heroes %}
<option value={{ hero.id }}>{{ hero.localized_name }}</option>
{% endfor %}
</select> <br>
<input type="submit" value="Submit">
</form>
嘗試接收時hero[id],當前輸出 null
{
"hero_id": null,
"steam32_id": ""
}
無論用戶選擇什么英雄。
我顯然誤解了一些東西;非常感謝任何指導。
作為參考,我使用的是OpenDota 的 API。
uj5u.com熱心網友回復:
我意識到我試圖訪問該欄位中的錯誤欄位select,而我的代碼中甚至不存在這樣的欄位。
我意識到select輸入的名稱是我所需要的,而不是value里面的option.
<select id="hero_list" name="hero_id">
{% for hero in heroes %}
<option value={{ hero.id }} >{{ hero.localized_name }}</option>
{% endfor %}
</select>
我只是重命名name了select輸入中的欄位。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360420.html
上一篇:為什么我的提交表單在帶有BalmUI的Vue3中不起作用
下一篇:表單發送錯誤資料
