【发布时间】:2017-04-16 01:47:43
【问题描述】:
我需要覆盖表单中的验证器并将我自己的验证器应用到这个特定的路径。
当上传的图片不是 jpg 时,它不会通过form.validate_on_submit() 检查,模板会在 html 中呈现错误。当我尝试使用时
raise StopValidation("image1 jpg only")
它会引发调试器并阻止我看到路由。我只是希望它与其他错误一起显示为 field.error。
@app.route('/test',methods=['GET','POST'])
def test():
form.image1.validators=[]
if request.method == "POST" and str(request.files['image1'].filename) != "":
if request.files['image1'].filename.split('.',1)[1] != "jpg" :
raise StopValidation("image1 jpg only")
print "image 1 not jpg"
if form.validate_on_submit():
# do stuff
return render_template('test.html')
【问题讨论】:
标签: python wtforms flask-wtforms