I made a edit product function where you can change stock, name, discount of the product but i cannot seem to retrieve the details and 'GET' method it in the form so that user does not have to retype the details they don't wish to change
I'm using flask forms and python jinja, to do this
This is my routes.py
@app.route('/updateProduct/<int:id>', methods=['GET','POST]'])
def update_product(id):
form = UpdateProduct()
product = AddProduct.query.get_or_404(id)
if request.method == "POST" and form.validate_on_submit():
product.name = form.name.data
product.price = form.price.data
product.stock = form.stock.data
product.discount = form.discount.data
product.desc = form.description.data
try:
db.session.add(product)
db.session.commit()
flash(f'Your product has been successfully updated!')
print("a product has been updated")
return redirect(url_for('retrieve_products', id=product.id))
except:
print("error updating product")
return redirect(url_for('retrieve_products', id=product.id))
form.name.data = product.name
form.price.data = product.price
form.stock.data = product.stock
form.discount.data = product.discount
form.description.data = product.desc
return render_template('retrieveProducts.html', form=form)
this is the edit form modal in my retrieveproducts.html
<td style="color:black">
<button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#myModal2_{{ product.id }}">
Edit
</button>
<div class="modal" id="myModal2_{{product.id}}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" style="color:black">Update Inventory</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal">X</button>
</div>
<div class="modal-body">
<form action="{{ url_for('update_product',id=product.id) }}" method="POST" enctype="multipart/form-data">
{{ form.hidden_tag() }}
<div class="form-group">
{{ form.name.label(class="form-control-label") }}
{{ form.name(class="form-control") }}
</div>
<div class="form-group">
{{ form.price.label(class="form-control-label") }}
{{ form.price(class="form-control") }}
</div>
<div class="form-group">
{{ form.discount.label(class="form-control-label") }}
{{ form.discount(class="form-control") }}
</div>
<div class="form-group">
{{ form.stock.label(class="form-control-label") }}
{{ form.stock(class="form-control") }}
</div>
<div class="form-group">
{{ form.description.label(class="form-control-label") }}
{{ form.description(class="form-control") }}
</div>
<div class="modal-footer">
{{ form.edit_submit(class="btn btn-primary") }}
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
</div>
</td>