How to delete a comment and return to the post/<id> page in flask
00:57 18 Apr 2022

I am working with my assignment recently and I facing a difficulty when I try to create a delete commment function. I just want to simplely delete the post's comment and return to the post page.

TypeError: delete_comment() got an unexpected keyword argument 'id'
127.0.0.1 - - [18/Apr/2022 13:57:36] "GET /delete_com/6 HTTP/1.1" 500 -

model.py

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(140))
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    post_comment = db.relationship('Comment', backref="post", lazy=True)

    def __repr__(self):
        return ''.format(self.body)

    def get_post(self):
        post = Post.query.filter(postsid=self.id)
        return post


class Comment(db.Model):
    comment_id = db.Column(db.Integer, primary_key=True)
    comment_username = db.Column(db.String(64), db.ForeignKey('user.username'))
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    comment = db.Column(db.String(140))

route.py

@bp.route('/post/', methods=['GET', 'POST'])
def post_page(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(comment=form.comment.data,
                          comment_username=current_user.username,
                          post_id=post.id)
        db.session.add(comment)
        db.session.commit()
        return redirect(url_for('main.post_page', id=id))
    page = request.args.get('page', 1, type=int)
    # list the corresponding comment in post
    comments = Comment.query.filter_by(post_id=post.id)
    pages = comments.paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.post_page', page=comments.next_num) \
        if pages.has_next else None
    prev_url = url_for('main.post_page', page=comments.prev_num) \
        if pages.has_prev else None
    return render_template('post.html', title=_('post comment'), form=form, post=post,
                           comments=comments, next_url=next_url,
                           prev_url=prev_url, pages=pages.items)


@bp.route('/delete_com/', methods=['GET', 'POST'])
@login_required
def delete_comment(comment_id):
    comment_to_delete = Comment.query.get_or_404(comment_id)
    try:
        db.session.delete(comment_to_delete)
        db.session.commit()
        flash(_('Comment was deleted'))
        return redirect(url_for('main.post_page'))
    except:
        flash("Oop! There was a problem, try again..")
        return render_template('post.html', title=_('post comment'))

_comment.html

    {% if current_user.username == comment.comment_username %}
    
    {% endif %}

I have no idea how to redirect to the front post_page/. under def delete_comment .L know it need to return a post.id to route back to the post page butdef delete_comment(comment_id):doesn't contain.

I also try to set two arguments in the function to provide the post.id for redirect but it give delete_comment() missing 1 required positional argument: 'comment_id'

def delete_comment(id, comment_id):
    post = Post.query.get_or_404(id)
    comment_to_delete = Comment.query.get_or_404(comment_id)

I have stuck in this part quite long time, can anyone help me please?

python html flask