我有一个表单,用户可以输入详细信息并提交并查看他们在我的网页上输入的内容但是,我想添加错误处理功能,如果用户没有在输入字段中输入任何内容,则显示和错误说please dont leave field empty
我有3个字段,我希望此功能单独应用于每个字段,因此如果需要,我可以取出一个,我在下面添加了我的代码:
Python代码
@app.route('/personal', methods = ['POST', 'GET'])
def personal():
query = "select * from guestbook5;"
cur = db.execute(query)
rv = cur.fetchall()
print(rv)
cur.close()
return render_template('personal.html',rv=rv)
@app.route('/personalform',methods=['GET'])
def personalform():
return render_template('personalform.html')
@app.route('/sign3', methods=['POST'])
def sign3():
country=request.form['Country']
relationship=request.form['Relationship']
kids=request.form['Kids']
params = {'Country':country, 'Relationship':relationship, 'Kids':kids }
db.execute("insert into guestbook5 values
(:Country,:Relationship,:Kids);",params)
db.commit()
return redirect(url_for('personal'))
html代码
<form action="{{ url_for('sign3') }}" method=post>
<dl>
<dt>Country:
<dd><input type=text name=Country>
<dt>Relationship:
<dd><input type=text name=Relationship>
<dt>Kids:
<dd><input type=text name=Kids>
<dd><input type=submit value=sign>
</dl>
</form>
答案 0 :(得分:0)
使用以下内容编辑sign3功能。我添加了一个if语句,说如果Country,Relationship或Kids为空,它会在HTML页面上向用户发送一条错误消息。 <"post error message">
用于填写错误消息并链接回HTML页面。
country=request.form['Country']
relationship=request.form['Relationship']
kids=request.form['Kids']
if country == '' or relationship == '' or kids == '':
<"post error message">
else:
params = {'Country':country, 'Relationship':relationship, 'Kids':kids }
db.execute("insert into guestbook5 values
(:Country,:Relationship,:Kids);",params)
db.commit()
return redirect(url_for('personal'))