Python displaying details in flask form when editing item so that user doesn't have to retype the same details

Viewed 307

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>

3 Answers

Try this:

@app.route('/updateProduct/<int:id>', methods=['GET','POST']) #  Remove the extra bracket



In your route, you did not passed product variable:

<form action="{{ url_for('update_product',id=product.id) }}" method="POST" enctype="multipart/form-data">

This should create an error when you submit the form as product.id does not exist.

[Added]

return render_template('retrieveProducts.html', form=form, product=product)

Although I prefer to pass id variable since the route already has it.

I think you don't have to write

db.session.add(product)
db.session.commit()

because adding means you are creating a new record in your database, so just remove the part: ' db.session.add(product) ' and keep only: ' db.session.commit() '

commit will update the existing content.

I hope this would help you, just try it a once and notify here.

Pass the product in your render template method as follows

return render_template('retrieveProducts.html', form=form, product=product)

Pass a value property in forms in order to get the current product details i.e

<div class="form-group">
              {{ form.stock.label(class="form-control-label") }}
              {{ form.stock(class="form-control", value=product.stock) }}
</div>
Related