I'm trying to add a comment section to my flask web app. My code works fine, but just in case I delete last comment was just added I got an apology (mut provide comment) I don't understand why I got this message while I'm trying to delete not to insert a comment. Any help would be great
Here is my python code
@app.route("/addcomment", methods=["GET", "POST"])
@login_required
def addcomment():
global book
if request.method == "GET":
book = request.args.get("book")
comments = db.execute("SELECT * FROM comment WHERE book_id=? ORDER BY Timestamp DESC",book)
return render_template ("comment.html", comments=comments)
return redirect (request.referrer)
else:
comment = request.form.get("comment")
print(comment)
id = session["user_id"]
if comment:
user = db.execute("SELECT username FROM users WHERE id=?", id)
db.execute("INSERT INTO comment (comment, user,book_id) VALUES(?, ?,?)",comment, user[0]["username"],book)
return redirect (request.referrer)
return apology("must provide comment", 403)
@app.route("/deletecomment/<comment_id>", methods=["GET", "POST"])
@login_required
def deletecomment(comment_id):
if request.method == "POST":
print(comment_id)
db.execute("DELETE FROM comment WHERE id=?",comment_id)
return redirect (request.referrer)
And here is comment.html
<form action="{{url_for('addcomment')}}" method="post">
<div class="row bootstrap snippets bootdeys">
<div class="col-md-8 col-sm-12">
<div class="comment-wrapper">
<div class="panel panel-info">
<div class="panel-heading">
Comment panel
</div>
<div class="panel-body">
<textarea name="comment" class="form-control" placeholder="write a comment..." rows="3"></textarea>
<br>
<button type="submit" class="btn btn-info pull-right">Post</button>
<div class="clearfix"></div>
<hr>
{% for comments in comments %}
<ul class="media-list">
<!--<button type="submit" class="btn btn-danger">Delete</button>-->
<li class="media">
<a href="#" class="pull-left">
<img src="https://bootdey.com/img/Content/user_1.jpg" alt="" class="img-circle">
</a>
<div class="media-body">
<!--<button type="submit" class="btn btn-danger">Delete</button>-->
<span class="text-muted pull-right">
</span>
<!--<button type="submit" class="btn btn-danger">Delete</button>-->
<strong class="text-success">{{comments.user}}</strong>
<form action="/deletecomment/{{comments.id}}" method="post">
<p>
{{comments.comment}}
</p>
<button id="but" type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</li>
</div>
</ul>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</form>