簡體   English   中英

在服務器上找不到請求的 URL。 如果您手動輸入 URL,請檢查您的拼寫並重試

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM