我有以下來自 bootstrap.org 的選擇表單并根據我的需要進行了自定義,但是當我使用 request.form() 時,輸出始終為空:
HTML
<form method="POST" action="{{ url_for('addPOTM') }}">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<label class="mr-sm-2" for="inlineFormCustomSelect">Add Player</label>
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>Choose...</option>
{% for rows in dropdown %}
<option value="{{ rows.Name }}">{{ rows.Name }}</option>
{% endfor %}
</select>
</div>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary" name="newPOTM">Submit</button>
</div>
</div>
</form>
</div>
Python:
import sqlite3, logging
from flask import Flask, render_template, request, url_for, flash, redirect, jsonify
from werkzeug.exceptions import abort
@app.route('/potm')
def potm():
conn = get_db_connection()
dragdrop = conn.execute("SELECT * FROM POTM ORDER BY listorder ASC")
dropdown = conn.execute('SELECT Name FROM alltime ORDER BY Name ASC').fetchall()
return render_template('potm.html', dragdrop=dragdrop, dropdown=dropdown)
@app.route("/addPOTM",methods=["POST","GET"])
def addPOTM():
conn = get_db_connection()
if request.method == 'POST':
Name = request.form['newPOTM']
print(Name)
return redirect(url_for('potm'))
感覺很簡單,我今天已經有了,但沒有保存,現在看不到我今天早些時候做了什么:(需要休息一下
uj5u.com熱心網友回復:
您正在閱讀以下值newPOTM:
if request.method == 'POST':
Name = request.form['newPOTM']
print(Name)
并且您的表單將它提交到一個按鈕元素中(具有空值):
<button type="submit" class="btn btn-primary" name="newPOTM">Submit</button>
當您確實需要讀取 select 元素的值時。只需將正確的name屬性添加到選擇中(并將其從 中洗掉button):
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="newPOTM">
uj5u.com熱心網友回復:
我也在行中添加了 name="newPOTM"
<select id="inlineFormCustomSelect">
它有效
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/325167.html
