我從 MySQL 資料庫中獲取值,并且可以在 HTML 上顯示它,但是當我嘗試發送值時,我總是得到第一個。
這是表格:
<form action="/delete_country" method="post">
<table>
<tr>
{% for header in headers %}
<th>{{header}}</th>
{% endfor %}
</tr>
{% for row in countries %}
<tr>
{% for cell in row %}
<td>{{cell}}</td>
{% endfor %}
<td><input type="text" name="id_country" value="{{row[0]}}"></td>
<td><button type="submit">Delete</button></td>
</tr>
{% endfor %}
</table>
</form>
這是 server.py 上總是回傳第一個值的函式
@app.route("/")
@app.route("/index")
def index():
headers = ("CODE", "Name", "Continent", "code2", "Delete")
countries = controller.get_coutries()
return render_template("index.html", headers=headers, countries=countries)
@app.route("/delete_country", methods=['POST'])
def delete_country():
id_country = request.form['id_country']
return id_country
控制器.py
def get_coutries():
con = get_connection()
countries = []
with con.cursor() as cursor:
cursor.execute("select code, name, continent from country")
countries = cursor.fetchall()
con.close()
return countries

如果我點擊 Antartica,代碼是 ATA 它會回傳 ABW。
uj5u.com熱心網友回復:
<td><input type="text" name="id_country" value="{{row[0]}}"></td>
您在每個回圈中設定相同的 id,因此您有很多具有相同 id 的輸入,這就是您獲得第一個值的原因
我更喜歡此類事物的鏈接而不是表單,并在燒瓶端使用 url 引數。
<a href="http://yourip/delete_country/{{row[0]}}"></a>
@app.route("/delete_country/<id>", methods=['POST'])
def delete_country(id):
id_country = id
return id_country
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442480.html
標籤:html python-3.x 形式 烧瓶
