Home > OS >  Python displaying details in flask form when editing item so that user doesn't have to retype t
Python displaying details in flask form when editing item so that user doesn't have to retype t

Time:02-14

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"  data-bs-toggle="modal" data-bs-target="#myModal2_{{ product.id }}">
            Edit
              </button>

              <div  id="myModal2_{{product.id}}">
                <div >
                  <div >
                    <div >
                      <h4  style="color:black">Update Inventory</h4>
                      <button type="button"  data-bs-dismiss="modal">X</button>
                    </div>
                    <div >
                  <form action="{{ url_for('update_product',id=product.id) }}" method="POST" enctype="multipart/form-data">
                      {{ form.hidden_tag() }}
              <div >
                  {{ form.name.label() }}
                  {{ form.name() }}
              </div>
              <div >
                  {{ form.price.label() }}
                  {{ form.price() }}

              </div>
              <div >
                  {{ form.discount.label() }}
                  {{ form.discount() }}

              </div>
              <div >
                  {{ form.stock.label() }}
                  {{ form.stock() }}

              </div>
                  <div >
                  {{ form.description.label() }}
                      {{ form.description() }}

                </div>


  <div >
                  {{ form.edit_submit() }}
                      <button type="button"  data-bs-dismiss="modal">Cancel</button>
                    </div>
        </form>

                    </div>

                  </div>
                </div>
              </div>
             </td>

CodePudding user response:

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.

CodePudding user response:

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.

  • Related