简体   繁体   中英

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

i am having and issue here in my blog website....i want the user to be able to comment under a post but it kept showning me that,i don't know what else to do i have tried but it kept repeating this

here is my route

@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))

my 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}')"

my form

 <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>

guys please help me out

Try adding this to your html:

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

Your action route in the form is wrong. It should be /create_comment/<post_id> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question FLASK - The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again python flask URL error: 'The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.' Flask - 404 Not found : The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again Flask: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again Flask: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again Why am I getting "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again" Flask error: 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. 404 Python Flask API : The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again Flask app - Error 404, The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM