Flask wtforms: validate_on_submit() returns true 每次
Flask wtforms: validate_on_submit() returns true every time
我是编程新手,想知道为什么 validate_on_submit() returns 每次都正确,即使表格未填写。我正在尝试构建社交网站(facebook、twitter)的副本,并正在尝试实现一种评论、回复和点赞 post 的方式。但是,每次我 "like" 一个 post 或评论,评论的副本都会被添加到数据库中。
这是我的代码:
在forms.py中:
from flask_wtf import FlaskForm
from wtforms import SubmitField, TextAreaField
from wtforms.validators import DataRequired
class PostForm(FlaskForm):
content = TextAreaField("Content", validators=[DataRequired()])
submit = SubmitField("Post")
class CommentForm(FlaskForm):
content = TextAreaField("Comment_content", validators=[DataRequired()])
submit = SubmitField("Comment")
class ReplyForm(FlaskForm):
content = TextAreaField("Reply_content", validators=[DataRequired()])
submit = SubmitField("Reply")
在routes.py中:
@posts.route("/post/<int:post_id>", methods=["GET","POST"])
@login_required
def post(post_id):
comment_form = CommentForm()
reply_form = ReplyForm()
post = Post.query.get_or_404(post_id)
image_file = url_for("static", filename="profile_pic" + current_user.image_file)
comments = Post_comment.query.order_by(Post_comment.date_commented.desc()).filter_by(post_id=post_id)
print(comment_form.validate_on_submit)
if "post_like" in request.form:
user_likes_post(request.form["post_like"])
elif "comment_like" in request.form:
user_likes_comment(request.form["comment_like"])
elif "reply_like" in request.form:
user_likes_reply(request.form["reply_like"])
if comment_form.validate_on_submit:
comment = Post_comment(content=comment_form.content.data, comment_author=current_user, post=post)
db.session.add(comment)
db.session.commit()
elif reply_form.validate_on_submit:
reply = Comment_reply(content=reply_form.content.data, reply_author=current_user)
db.session.add(reply)
db.session.commit()
return render_template("post.html", post=post, comment_form=comment_form, reply_form=reply_form, image_file=image_file, comments=comments)
在post.html中:
{% extends "layout.html" %}
{% block content %}
<div id="post">
<div id="post_desc">
<img src="{{ image_file }}">
<a>{{ post.author.username }}</a>
{{ post.date_posted }}
</div>
<div id="post_content">
{{ post.content }}
</div>
<div>{{ post.like_count }}</div>
<form method="POST">
<button name="post_like" type="submit" value="{{ post.id }}" >like</button>
<button name="comment" type="button" href="#" >comment</button>
</form>
</div>
<div>
<form method="POST" action="">
{{ comment_form.hidden_tag() }}
{{ comment_form.csrf_token }}
<fieldset>
<div>
{% if comment_form.content.errors %}
{{ comment_form.content() }}
<div>
{% for error in comment_form.content.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ comment_form.content() }}
{% endif %}
</div>
<div>
{{ comment_form.submit() }}
</div>
</form>
{% for comment in comments %}
<div id="comment">
<div id="comment_desc">
<img src="{{ image_file }}">
<a>{{ comment.comment_author.username }}</a>
{{ comment.date_posted }}
</div>
<div id="comment_content">
{{ comment.content }}
</div>
<div>{{ comment.like_count }}</div>
<form method="POST">
<button name="comment_like" type="submit" value="{{ comment.id }}" >like</button>
</form>
</div>
<div>
<form method="POST" action="">
{{ reply_form.hidden_tag() }}
{{ reply_form.csrf_token }}
<fieldset>
<div>
{% if reply_form.content.errors %}
{{ reply_form.content() }}
<div>
{% for error in reply_form.content.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ reply_form.content() }}
{% endif %}
</div>
<div>
{{ reply_form.submit() }}
</div>
</form>
</div>
{% for reply in comment.replies %}
<div id="reply">
<div id="reply_desc">
<img src="{{ image_file }}">
<a>{{ reply.reply_author.username }}</a>
{{ reply.date_posted }}
</div>
<div id="reply_content">
{{ reply.content }}
</div>
<div>{{ reply.like_count }}</div>
<form method="POST">
<button name="reply_like" type="submit" value="{{ reply.id }}" >like</button>
</form>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
{% endblock %}
我认为这里发生的事情是,当我 "like" post、评论或回复时,发送了一个 POST 请求,但我不确定为什么 validate_on_submit() returns 即使有 Datarequired() 验证器,它也能通过 if 语句并将条目添加到数据库。
抱歉,如果我的代码很乱,我是编程新手,需要一些时间来学习最佳实践。
看来您可能有语法问题,
这是你拥有的:
if comment_form.validate_on_submit:
这就是 documentation 所说的应该使用的方式:
if comment_form.validate_on_submit():
希望对您有所帮助! :D
我是编程新手,想知道为什么 validate_on_submit() returns 每次都正确,即使表格未填写。我正在尝试构建社交网站(facebook、twitter)的副本,并正在尝试实现一种评论、回复和点赞 post 的方式。但是,每次我 "like" 一个 post 或评论,评论的副本都会被添加到数据库中。
这是我的代码:
在forms.py中:
from flask_wtf import FlaskForm
from wtforms import SubmitField, TextAreaField
from wtforms.validators import DataRequired
class PostForm(FlaskForm):
content = TextAreaField("Content", validators=[DataRequired()])
submit = SubmitField("Post")
class CommentForm(FlaskForm):
content = TextAreaField("Comment_content", validators=[DataRequired()])
submit = SubmitField("Comment")
class ReplyForm(FlaskForm):
content = TextAreaField("Reply_content", validators=[DataRequired()])
submit = SubmitField("Reply")
在routes.py中:
@posts.route("/post/<int:post_id>", methods=["GET","POST"])
@login_required
def post(post_id):
comment_form = CommentForm()
reply_form = ReplyForm()
post = Post.query.get_or_404(post_id)
image_file = url_for("static", filename="profile_pic" + current_user.image_file)
comments = Post_comment.query.order_by(Post_comment.date_commented.desc()).filter_by(post_id=post_id)
print(comment_form.validate_on_submit)
if "post_like" in request.form:
user_likes_post(request.form["post_like"])
elif "comment_like" in request.form:
user_likes_comment(request.form["comment_like"])
elif "reply_like" in request.form:
user_likes_reply(request.form["reply_like"])
if comment_form.validate_on_submit:
comment = Post_comment(content=comment_form.content.data, comment_author=current_user, post=post)
db.session.add(comment)
db.session.commit()
elif reply_form.validate_on_submit:
reply = Comment_reply(content=reply_form.content.data, reply_author=current_user)
db.session.add(reply)
db.session.commit()
return render_template("post.html", post=post, comment_form=comment_form, reply_form=reply_form, image_file=image_file, comments=comments)
在post.html中:
{% extends "layout.html" %}
{% block content %}
<div id="post">
<div id="post_desc">
<img src="{{ image_file }}">
<a>{{ post.author.username }}</a>
{{ post.date_posted }}
</div>
<div id="post_content">
{{ post.content }}
</div>
<div>{{ post.like_count }}</div>
<form method="POST">
<button name="post_like" type="submit" value="{{ post.id }}" >like</button>
<button name="comment" type="button" href="#" >comment</button>
</form>
</div>
<div>
<form method="POST" action="">
{{ comment_form.hidden_tag() }}
{{ comment_form.csrf_token }}
<fieldset>
<div>
{% if comment_form.content.errors %}
{{ comment_form.content() }}
<div>
{% for error in comment_form.content.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ comment_form.content() }}
{% endif %}
</div>
<div>
{{ comment_form.submit() }}
</div>
</form>
{% for comment in comments %}
<div id="comment">
<div id="comment_desc">
<img src="{{ image_file }}">
<a>{{ comment.comment_author.username }}</a>
{{ comment.date_posted }}
</div>
<div id="comment_content">
{{ comment.content }}
</div>
<div>{{ comment.like_count }}</div>
<form method="POST">
<button name="comment_like" type="submit" value="{{ comment.id }}" >like</button>
</form>
</div>
<div>
<form method="POST" action="">
{{ reply_form.hidden_tag() }}
{{ reply_form.csrf_token }}
<fieldset>
<div>
{% if reply_form.content.errors %}
{{ reply_form.content() }}
<div>
{% for error in reply_form.content.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ reply_form.content() }}
{% endif %}
</div>
<div>
{{ reply_form.submit() }}
</div>
</form>
</div>
{% for reply in comment.replies %}
<div id="reply">
<div id="reply_desc">
<img src="{{ image_file }}">
<a>{{ reply.reply_author.username }}</a>
{{ reply.date_posted }}
</div>
<div id="reply_content">
{{ reply.content }}
</div>
<div>{{ reply.like_count }}</div>
<form method="POST">
<button name="reply_like" type="submit" value="{{ reply.id }}" >like</button>
</form>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
{% endblock %}
我认为这里发生的事情是,当我 "like" post、评论或回复时,发送了一个 POST 请求,但我不确定为什么 validate_on_submit() returns 即使有 Datarequired() 验证器,它也能通过 if 语句并将条目添加到数据库。
抱歉,如果我的代码很乱,我是编程新手,需要一些时间来学习最佳实践。
看来您可能有语法问题,
这是你拥有的:
if comment_form.validate_on_submit:
这就是 documentation 所说的应该使用的方式:
if comment_form.validate_on_submit():
希望对您有所帮助! :D