0
用戶輸入他們的地址和年齡。當查找按鈕被擊中時,它將顯示輸入。但是,對於輸入無效的情況,我想添加一條錯誤消息。檢查用戶輸入是否有效
如何檢查輸入是否無效並給出錯誤消息?我希望我的錯誤消息與輸入值相同的頁面上。代碼無法正常工作。
<div class = "Information">
<h2>Your Infomation</h2>
<form action="" method=post>
if (error){
<p id="error"><strong></strong>{{error}}
}
<div id = "address">
<label>address</label>
<input type=text name=address value="{{request.form.address}}">
</div>
<div id = "age">
<label>age</label>
<input type=text name=age value="{{request.form.age}}">
</div>
<div class = "Input">
<a href="/results">
<button type = "button" class = "btn btn-primary" value="find" >Find!</button>
</a>
</div>
</form>
</div>
@app.route('/results',methods=['GET','POST'])
def result():
error = None;
if request.method == 'POST':
if request.form['age'] != '14':
error='You did not enter proper values'
return render_template('template.html',error=error)
由於它的工作原理 – FamilyMaze
你能告訴我爲什麼如果request.method ==「POST」:當我年齡鍵入14不會被調用? – FamilyMaze