[英]Use 2 inputs from different html site for one app.route?
您好,我對 Python Flask 申請有疑問。 我一直在尋找,但找不到合適的解決方案..
我想使用輸入從該數據集中搜索某些列。 然后我想將這些列與另一個輸入 foat 輸入相乘並將它們打印在'calculate.html'上
我有一個看起來像這樣的數據集:
df=
A B(m) C(cm) D(m)
house 50 50 100
bedroom 80 50 600
bed 20 60 500
我有 2 個不同的 html 頁。 第一頁應該通過輸入 1。應該在數據集中搜索輸入。 什么都不應該打印出來。
索引.html:
<form> action ='/index' method = 'post'>
<p>< input type = 'text' name='inpt'/> </p>
<p>< input type = 'submit' name='submit'/> </p>
在第 2 頁 HTML,然后應給出要相乘的數字。 而且它還必須在此站點上打印此程序的輸出。
計算.html
<h1>{{inpt}}</h1>
<form> action ='/index' method = 'post'>
<p>< input type = 'text' name='inpt2'/> </p>
<p>< input type = 'submit' name='submit'/> </p>
<h3>{{my_column}}</h3>
<h3>{{my_numbers}}</h3>
這是我的 Python 腳本:
@app.route('/index', methods=['POST', 'GET'])
def index():
my_numbers = []
my_column = []
if request.method == 'POST'
inpt = request.form['inpt']
inpt2 = request.form['inpt2']
output = False
for text in df['A']:
if text == inpt:
give_text = df[df['A'] == inpt]
give_text = give_text.drop(['A'], axis = 1)
output = True
if output == True:
return render_template('calculate.html', inpt = inpt)
# At this point the program should redirect to 'calculate.html' in order to continue calculating there
# Here we start to calculate with inpt2
for column in give_text:
column_edit = give_text[column]
my_columns.append(column)
column_edit = float(column_edit) * float(inpt2)
if '(m)' in column:
column_edit = column_edit + 'meter'
my_numbers.append(column_edit)
elif 'cm' in column:
column_edit= column_edit + 'centimeter'
my_numbers.append(column_edit)
return render_template('calculate.html', my_numbers = my_numbers, my_column = my_column)
else:
return render_template('index.html', content = 'No result')
但我不明白為什么它不起作用。 不能在一個 app.route 中使用來自 2 個不同站點的 2 個輸入?
你的縮進搞砸了。 我最好的猜測是它看起來像這樣:
if output == True:
return render_template('calculate.html', inpt = inpt)
# At this point the program should redirect to 'calculate.html' in order to continue calculating there
# Here we start to calculate with inpt2
for column in give_text:
column_edit = give_text[column]
my_columns.append(column)
column_edit = float(column_edit) * float(inpt2)
if '(m)' in column:
column_edit = column_edit + 'meter'
elif 'cm' in column:
column_edit= column_edit + 'centimeter'
return render_template('calculate.html', my_numbers = my_numbers, my_column = my_column)
else:
return render_template('index.html', content = 'No result')
請注意for
塊和隨后的return
以及else
子句都被縮進了一級。 以前,永遠不會到達for
塊,因為之前直接有一個return
,它和else
子句將在output != True
時執行,而不是在request.method != "POST"
時執行,我再次假設它是什么你打算。
正如評論中提到的那樣,您最好將代碼拆分得更多,以使其更易於遵循,並且更難犯這樣的錯誤。 它不一定是單獨的路由,但可能由不同的函數完成 GET 和 POST 處理。
關於如何將路線分成 2 條分開的路線
index.html
<form action ='/index' method='post'>
<p><input type='text' name='inpt'/> </p>
<p><input type='submit' name='submit'/> </p>
</form>
(我現在意識到你沒有用</form>
關閉你的表單並且打開標簽<form>
也是格式錯誤的)
計算.html
<h1>{{inpt}}</h1>
<form action='/calculate' method='post'>
<input type='text' value='{{inpt}}' style='display:none;'>
<p>< input type = 'text' name='inpt'/> </p>
<p>< input type = 'submit' name='submit'/> </p>
<h3>{{my_column}}</h3>
<h3>{{my_numbers}}</h3>
</form>
查看.py
@app.route('/index', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/index', methods=['POST'])
def index_post():
my_numbers = []
my_column = []
inpt = request.form['inpt']
output = False
for text in df['A']:
if text == inpt:
give_text = df[df['A'] == inpt]
give_text = give_text.drop(['A'], axis = 1)
output = True
if output == True:
return render_template('calculate.html', inpt=inpt)
else:
return render_template('index.html', content='No result')
@app.route('/calculate', methods=['POST'])
def calculate():
my_numbers = []
my_column = []
inpt = request.form['inpt']
inpt2 = request.form['inpt2']
output = False
for text in df['A']:
if text == inpt:
give_text = df[df['A'] == inpt]
give_text = give_text.drop(['A'], axis = 1)
# Here we start to calculate with inpt2
for column in give_text:
column_edit = give_text[column]
my_columns.append(column)
column_edit = float(column_edit) * float(inpt2)
if '(m)' in column:
column_edit = column_edit + 'meter'
my_numbers.append(column_edit)
elif 'cm' in column:
column_edit= column_edit + 'centimeter'
my_numbers.append(column_edit)
return render_template('calculate.html', my_numbers=my_numbers, my_column=my_column, inpt=inpt)
另外,正如@Adam Barnes所說,您的縮進有點亂,所以我不太確定inpt2
的部分
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.