李老师您好!
经您提醒后,我尝试了给StringField添加Datarequired的验证器,这种情况下,当用户没有填写信息,网页不会再刷新。但是当我把表单换成RadioField并添加DataRequired的验证器之后,网页还是刷新了。我在stackoverflow上找到了这样一个回答:python - Flask-WTForms RadioField custom validator message doesn't work - Stack Overflow
似乎这是一个flask的一个bug?
另外,我按照您上次提供的方法,将session放在表单验证并提交之后,但是并没有奏效
这次我在session中添加了[‘page_num’],希望能以此实现翻页功能。期望的效果是,如果将图片的链接(img_url),用户的选择(option)存入数据库或写入本地文档,这两个字段可以对应,并且在页数上+1,当页数到达设定的最大值时,跳转到结束页面。然而现在出现的情况是,用户的选择对应的并不是他对当前看到的图所做的选择,我将图片的url打印出来,发现图片在我提交表单的过程中出现了两次刷新。
代码如下:
demo.py
@app.route('/')
@app.route('/home',methods=['POST','GET'])
def home():
form=myForm()
name='Welcome to the "Online Recipe Origin" Survey'
letters = string.ascii_letters
user_id = ''.join(random.sample(letters, 15))
if request.method == 'POST':
user_id = request.form['user_id']
session['user'] = user_id
session['page_num'] = 0
#print(session['user'])
return redirect(url_for('random_image'))
return render_template('home.html',name=name,form=form,user_id=user_id)
@app.route('/end',methods=['POST','GET'])
def end():
name='Thanks!'
return render_template('end.html',name=name)
@app.route('/random_image',methods=['POST','GET'])
def random_image():
form=testform()
names = os.listdir(os.path.join(app.static_folder, 'images'))
img_url = url_for('static', filename=os.path.join('images', random.choice(names)))
print(img_url)
if form.validate_on_submit():
while session['page_num']<5: # set the max page_num
session['option'] = form.examples.data
#session['img_url'] = img_url
session['page_num'] +=1
print(session)
return redirect('random_image') # claer the choice the user made
else:
print(form.errors)
print(session)
return redirect('end')
return render_template('random_image.html',img_url=img_url,form=form)
if __name__ == '__main__':
app.run(debug=True)
form.py
class myForm(Form):
user = StringField('user',validators=[Length(min=4,max=25),DataRequired()])
user_id= HiddenField()
class testform(Form):
examples = RadioField('portals', choices=[('1', 'Option1'), ('2', 'Option2'), ('3', 'Option3')],coerce=str,validators=[DataRequired()])
BooleanField('I accept the TOS', [validators.DataRequired()])
random_image.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Recipe Survey</title>
</head>
<body>
<div class="container" style="margin-top: 50px">
<div align="center">
<h2>Welcome to the "Online Recipe Origin" Survey</h2>
<form method="POST" action="">
{{ form.hidden_tag() }}
{% for error in form.examples.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
<img src="{{ img_url }}">
<h5>{{ img_url }}</h5>
{{ form.examples() }}
<button type="submit" class="'btn btn-lg btn-success">NEXT</button>
</form>
</div>
</div>
</body>
</html>