请求的URL在服务器上找不到.如果您手动输入URL,请检查拼写并重试



我在我的博客网站....有和问题我想让用户能够在帖子下发表评论,但它一直显示我,我不知道还能做什么,我试过了,但它一直重复这个

这是我的路线

@posts.route("/create-comment/<post_id>", methods=['GET', 'POST'])
@login_required
def create_comment(post_id):
text = request.form.get['text']
if not text:
flash('Comment cannot be empty.', 'danger')
else:
post = Post.query.filter_by(post_id)
if post:
comment = Comment(text=text, post_id=post_id, author=current_user)
db.session.add(comment)
db.session.commit()
else:
flash('Post not found.', 'danger')

return redirect(url_for('posts.post', post_id=post_id))

我的模型
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
Comment = db.relationship('Comment', backref='author', lazy=True)

def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.image_file}')"
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)
Comment = db.relationship('Comment', backref='post', 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)
content = db.Column(db.Text(200), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
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.content}', '{self.date_posted}')"

我的形式
<form class="input-group mb-3" method="POST" action="/create_comment">
<input type="text" id="text" class="form-control" placeholder="Comment" name="comment">
<div class="input-group-append">
<button type="submit" class="btn btn-outline-secondary" >Post</button>
</div>
</form>

伙计们,帮帮我吧

尝试将此添加到您的html:

<form action="{{ url_for('create_comment', post_id=post.id) }}" method = "POST">

表单中的动作路由错误。应该是/create_comment/<post_id>

最新更新