[英]FLASK - The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again
[英]The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again
我在我的博客网站上有问题....我希望用户能够在帖子下发表评论,但它一直向我展示,我不知道还能做什么我已经尝试过但它一直重复这个
这是我的路线
@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))
我的 model
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 = action="{{ url_for('create_comment', post_id=post.id) }}" method = "POST">
您在表单中的操作路线是错误的。 它应该是/create_comment/<post_id>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.