嗨,我是网络开发新手。我正在使用烧瓶,我有这个 ff。错误:
“无法为端点“post”构建 url。您是否忘记指定值 ['post_id']?”
我正在尝试在删除帖子中的评论后重定向页面。但它把我带到了“500 Internal Server Error”页面,当我单击浏览器的后退按钮时,会显示成功的 flash 消息,并且评论被删除。
"routes.py"
@app.route("/post/<int:post_id>")
@login_required
def post(post_id):
post = Post.query.get_or_404(post_id)
return render_template("post.html", title="Post", post=post)
@app.route("/post-comment/<int:post_id>", methods=['GET','POST'])
@login_required
def comment_post(post_id):
post = Post.query.get_or_404(post_id)
form = CommentForm()
if form.validate_on_submit():
comment = Comment(body=form.body.data, post_id=post.id, author=current_user)
db.session.add(comment)
db.session.commit()
flash("Your comment has been added to the post", "success")
return redirect(url_for("comment_post", post_id=post.id, post=post))
return render_template("comment.html", title="Comment Post", post_id=post.id, post=post, form=form)
@app.route("/comment-delete/<int:comment_id>", methods=['POST'])
@login_required
def delete_comment(comment_id):
comment = Comment.query.filter_by(id=comment_id).first()
if comment.author != current_user:
abort(403)
db.session.delete(comment)
db.session.commit()
flash('Your comment has been deleted!', 'success')
return redirect(url_for('post'))
“post.html 和 comment.html” - 两者都有用于删除评论的 url_for
{% for comment in post.comments %}
<table>
<tr>
<td>
{% if comment.author == current_user %}
<form action="{{url_for('delete_comment', comment_id=comment.id, post_id=post.id, post=post)}}" method="POST">
<input class="btn btn-danger" type="submit" value="Delete">
</form>
{% endif %}
<small class="text-muted">{{ comment.author.username }}</small>
<p class="mb-1">{{ comment.body }}</p>
</td>
</tr>
</table>
{% endfor %}
数据库
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
comments = db.relationship('Comment', backref='author', lazy=True)
posts = db.relationship('Post', backref='author', lazy=True)
def __repr__(self):
return f"User('{self.username}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
comments = db.relationship("Comment", backref="post", cascade="all, delete-orphan", lazy=True)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
def __repr__(self):
return f"Comment('{self.body}')"
在
delete_comment
函数中,当您重定向到 post
函数时,您还必须传递 post_id
。
它应该是这样的:
return redirect(url_for('post', post_id=post.id))
为此,您必须从数据库中获取帖子 ID。
我遇到了这种错误:“无法使用值 ['post_id'] 为端点 'update_post' 构建 url。您的意思是 'post_post' 吗?” 在Python Flask项目中。但问题是我没有在我的routes.py中定义“def update_post”。