I am using flask for my website and want to update an object and when I call the function for the action instead of POST I get the GET request and can't get the execution right this is part of my home.html :
{% for bars in user.barnames %}
<li class = "list-group-item">
<h1 name="barnameId" id="barnameId" > {{ bars.data }} </h1>
<form method="post">
<div align="right">
<a href="/update/{{bars.id}}">
<input type="button" class = "btn btn-primary" value="update" /></a>
<a href="/delete/{{bars.id}}">
<input type="button" class = "btn btn-primary" value="delete" /></a>
</div>
</form>
</li>
{% endfor %}
this is update.html :
<p>{{bars.data}}</p>
<form action="/update/{{bars.id}}" method = "post" >
<input type="text" name = "sender" id = "sender" class = "form-control" value="{{bars.sender}}">
<div align = "center">
<button type = "submit" class = "btn btn-primary" >update</button>
</div>
</form>
this is update function :
@views.route("/update/<int:id>" , methods = ["post" , "get"])
@login_required
def update(id):
bars = Barname.query.get_or_404(id)
if request.method == "post" :
bars.sender= request.form.get("sender")
try:
db.session.commit()
return render_template("home.html" , user = current_user)
except :
flash ("something went wrong" , category = "error")
return render_template("home.html" , user = current_user)
else :
flash("method was get" , category = "error")
#1
return render_template("update.html" , user = current_user , bars=bars)
so if I use this I keep getting method was get and go to the update page , if I press the update button in update.html nothing gonna happen and object is unchanged and still the request is GET if I use:
bars.sender= request.form.get("sender")
db.session.commit()
instead of #1 I can update the object but entering the update page I get None as sender (which make sense) ,I can update the object but still the request is GET and also obviously if I go back to home and want to update again I get None as sender so the execution is wrong also if I use :
request.form["sender"]
instead of :
request.form.get("sender")
I get werkzeug.exceptions.BadRequestKeyError: 400 Bad Request
I'm new to coding so sorry if my code and question is dumb