【问题标题】:How to access value from select tag using Flask?如何使用 Flask 从选择标签中访问值?
【发布时间】:2015-07-31 18:46:49
【问题描述】:

我正在尝试使用 Flask 获取我的选择标签中的值。我尝试这样做是为了使用输入标签并且它可以工作,但我不确定如何使用选择标签来做到这一点。 使用此 HTML 代码:

<form method="POST">
    <p>
        <input type="text" name="input">
        <input type="submit" value="Submit">
    </p>
</form>

还有这个python代码:

@app.route('/',methods=['POST'])
def run():
    printHTML(request.form['input'])
    return render_template('home.html')

我可以使用烧瓶访问输入文本,但由于某种原因,当我尝试对 select 标记执行相同操作时,它不起作用。

HTML 代码:

<form method="POST"> 
<select name ="startYear">
    <option value="" disabled="disabled"  style="display:none" selected="selected">Please select a year</option>
    <option value="2015">2015</option>
    <option value="2016">2016</option>
    <option value="2017">2017</option>
    <option value="2018">2018</option>
    <option value="2019">2019</option>
    <option value="2020">2020</option>
</select>
<select name ="startMonth">
    <option value="" disabled="disabled"  style="display:none" selected="selected">Please select a month</option>
    <option value="01">January</option>
    <option value="02">February</option>    
    <option value="03">March</option>                       
    <option value="04">April</option>
</select>
</form>

还有这个python代码:

    from flask import Flask, render_template, redirect, request
from xml_identifiersearch import searchXML, printHTML
from xml_time import sortDate, returnDateFile, dateRange, searchXML, printDateHTML

app = Flask(__name__)

@app.route('/') 
def index():
    return render_template('home.html')       

@app.route('/',methods=['POST'])
def run():
    printHTML(request.form['input'])
    return render_template('home.html')

@app.route('/',methods=['POST'])
def run2():
    startYear=request.form['startYear']
    startMonth=request.form['startMonth']
    startDay=request.form['startDay']
    startHour=request.form['startHour']
    startMinute=request.form['startMinute']
    startSecond=request.form['startSecond']
    startMillisecond=request.form['startMillisecond']
    endYear=request.form['endYear']
    endMonth=request.form['endMonth']
    endDay=request.form['endDay']
    endHour=request.form['endHour']
    endMinute=request.form['endMinute']
    endSecond=request.form['endSecond']
    endMillisecond=request.form['endMillisecond']
    startDatetime=startYear+"-"+startMonth+"-"+startDay+"T"+startHour+":"+startMinute+":"+startSecond+"."+startMillisecond+"Z"
    endDatetime=endYear+"-"+endMonth+"-"+endDay+"T"+endHour+":"+endMinute+":"+endSecond+"."+endMillisecond+"Z"
    printDateHTML(startDatetime, endDatetime)
    return render_template('home.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000,debug=True)

这不允许我访问值 startYear 和 startMonth。

【问题讨论】:

  • run 和 run2() 在你的路线中吗?还是在使用 select 尝试模板时只有 run2()
  • 我已经编辑了我的OP,你现在应该可以看到整个python代码了

标签: python html flask


【解决方案1】:

我想通了。它不起作用,因为我在同一个 html 页面上有两个表单。如果我只是将它们放在一个表单标签中,它就起作用了。

【讨论】:

  • 我正想问你是保留这两种表格还是在两次测试之间只留下一张。很高兴你想通了
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 2016-07-28
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
相关资源
最近更新 更多