Home > Mobile >  The requested URL was not found on the server. If you entered the URL manually please check your spe
The requested URL was not found on the server. If you entered the URL manually please check your spe

Time:12-28

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  method="POST" action="/create_comment">
        <input type="text" id="text"  placeholder="Comment" name="comment">
        <div >
          <button type="submit"  >Post</button>
        </div>
 </form>

guys please help me out

CodePudding user response:

Try adding this to your html:

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

CodePudding user response:

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

  • Related