簡體   English   中英

如何使用Flask從select標簽訪問值?

[英]How to access value from select tag using Flask?

我正在嘗試使用Flask在我的select標簽內獲取值。 我嘗試使用輸入標簽來做到這一點,但它可以工作,但是我不確定如何使用選擇標簽來做到這一點。 使用以下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')

我可以使用flask訪問輸入文本,但是由於某些原因,當我嘗試使用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。

我想到了。 它沒有用,因為我在同一個html頁面上有兩種形式。 如果我只是將它們放到一個表單標簽中,那就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM