[英]Flask WTForms Validate on Submit not working
我正在我的应用程序中的一个页面上工作,其中用户使用 WTForms 在评论页面上提交评论,单击提交按钮后,应显示文本“成功”。 目前没有发生这种情况,因为点击提交后,评论页面会重新加载,但会出现错误。 我已经创建了其他需要表单验证的页面,除了这个页面,我似乎无法弄清楚原因,尽管从其他页面复制了代码。 任何见解都非常感谢!
错误图片截图
这是我的 HTML 代码
<html>
<body>
{% for books in books %}
{{books.title}}
{% endfor %}
<form action = "{{ url_for('review', isbn=books.isbn)}}", method = "POST">
<div>
<br>{{ form.review(class_='form-control',placeholder ='Leave a review here')}}</br>
<ul>
{% for error in form.review.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
<br>{{ form.rating(class_='form-control',placeholder ='Leave a rating here')}}</br>
<ul>
{% for error in form.rating.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{{form.csrf_token}}
{{form.submit_button }}
</div>
</form>
</body>
</html>
Python代码
@app.route("/review/<string:isbn>", methods = ['GET', 'POST'])
@login_required
def review(isbn):
review = Books.query.filter_by(isbn=isbn).all()
review_form = ReviewForm()
if review_form.validate_on_submit():
return "Success"
return render_template("review.html", books = review, form = review_form)
WTForms 字段
class ReviewForm(FlaskForm):
""" Review """
review = StringField('review_label', widget=TextArea(), validators = [InputRequired(message = "Review can't be empty")])
rating = StringField('rating_label', validators= [InputRequired(message="Please input a rating")])
submit_button = SubmitField('Submit')
我建议如下。 让我知道这是否有帮助! :)
from markupsafe import escape
@app.route("/review/<string:isbn>", methods = ['GET', 'POST'])
@login_required
def review(isbn):
review = Books.query.filter_by(isbn=isbn).all()
review_form = ReviewForm()
if review_form.validate_on_submit():
return "Success %s" % escape(isbn)
return render_template("review.html", books = review, form = review_form)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.