trying to update an object from my db but getting error or nothing happen this is my update func:
@views.route("/update/<int:id>" , methods = [ "GET" , "POST"])
@login_required
def update(id):
bars = Barname.query.get_or_404(id) #1
if request.method == "POST" :
try:
bars.data= request.form["sender"] #2
db.session.commit()
flash ("updated successfully" , category = "success")
return render_template("home.html" , user = current_user )
except :
flash ("something went wrong" , category = "error")
return render_template("home.html" , user = current_user)
else :
return render_template("update.html" , user = current_user , bars=bars )
my GET request send me to the update.html and POST request should update the object, after completing the form and submit it it pops off the "updated successfully " but doesn't commit it to db , I tried to change the location of #1 and move it before "try" and after "try" and also after else statement but either get an error or "except" execute also I changed #2 to request.form.get("sender") but also didn't work the problem is even if db.session.commit() execute it doesn't change the db. idk if it's necessary but this is my home.html and update.html maybe there is something wrong with them(!!!):
{% extends "base.html" %} {% block title %}{{bars.data}}{% endblock %}{% block content %}
<div class="form">
<form action="/update/{{bars.id}}" method = "POST" >
<input type="text" name = "sender" id = "sender" placeholder="{{bars.data}}" ><br/><br/>
<div align = "center">
<input type="submit" class="btn btn-primary" value="update" >
</div>
</form>
</div>
{% endblock %}
home.html:
<ul class = "list-group list-group-flush" id = "notes">
{% for bars in user.barnames %}
<li class = "list-group-item">
<h1 name="barnameId" id="barnameId" > {{ bars.data }} </h1>
<div align="right">
<a href="/delete/{{bars.id}}">
<input type = "button" class="btn btn-primary" value="delete" ></a>
<a href="/update/{{bars.id}}" method="POST">
<input type="button" class = "btn btn-primary" value="update" > </a>
</div>
</li>
{% endfor %}
</ul>