当我返回此选项时,我收到了错误的值。例如,当我发送n =" Arnold,Robert | ID:1"作为一个选项,它显示在网页上,但当用户点击提交时,只有"阿诺德,"被发送到我的Flask服务器。我试图获取ID值。
有没有人知道如何从表单中检索整个选项值或以其他方式从字符串中获取ID?
这是HTML
<h2>Donors</h2>
<form method="POST" action="/display_donor">
<select name="selected_donor">
{% for n in donors %}
<option value={{n}}>{{n}}</option>
{% endfor %}
</select>
<br />
<br><input type="submit" value="View donor"/>
</form>
这里是相关的Flask功能
@app.route('/display_donor', methods=['POST'])
def display_donor():
temp_id = request.form['selected_donor']
print "temp_id"
print temp_id
final_id = re.sub('[^0-9]','', temp_id)
print "final_id"
print final_id
display_value = 1
cursor = g.conn.execute("SELECT name,donor_id FROM donors")
donors = []
for result in cursor:
temp_string = result['name'] + " | ID: " +result['donor_id']
donors.append(temp_string)
# can also be accessed using result[0]
cursor.close()
print "this"
print final_id
#Returns a specific instance of transaction as specified by user
cursor = g.conn.execute("SELECT * FROM donors D WHERE D.donor_id='%s'" % final_id)
values = []
for result in cursor:
values.append(result['donor_id'])
values.append(result['name'])
values.append(result['donor_type'])
cursor.close()
#denote attribute names
attribute = []
attribute.append('donor_id')
attribute.append('name')
attribute.append('donor_type')
return render_template("donors.html",attribute=attribute,values=values,donors=donors,display_value=display_value)
答案 0 :(得分:0)
我通过单独发送值而不是单个字符串来解决此问题。这让我摆脱了所有的解析,并允许我打印出这样的值:
<h2>Donors</h2>
<form method="POST" action="/display_donor">
<select name="selected_donor">
{% for n in donor_ids %}
<option value={{n}}>{{ donor_names[loop.index0] }} | ID: {{n}}</option>
{% endfor %}
</select>
<br />
<br><input type="submit" value="View donor"/>
</form>