1

I am doing a Flask application. Everything is working fine there but there's a problem occurring while a form tries to insert values in the database. I debugged the code and saw that the request.method == 'POST' in add_warehouse() is not getting triggered.

aap.py

# Show Warehouse stocks
@app.route('/dashboard/warehouse', methods=['POST','GET'])
@is_logged_in
def warehouse():
    form = Add_Warehouse(request.form)
    cur = mysql.connection.cursor()
    result = cur.execute('SELECT * from company_warehouse')
    data = cur.fetchall()
    if result>0:
        return render_template('warehouse.html', stocks=data, form=form)
    else:
        return render_template('warehouse.html',msg='Stock Empty', form=form)
    cur.close()

# Add Warehouse Stocks
class Add_Warehouse(Form):
    product_name = StringField('Name',[validators.Length(min=5,max=20), validators.DataRequired()])
    product_qty = IntegerField('Quantity',[validators.DataRequired()])
    product_price = DecimalField('Price',[validators.DataRequired()])

@app.route('/add_warehouse',methods=['POST','GET'])
@is_logged_in
def add_warehouse():
    form = Add_Warehouse(request.form)
    if request.method == 'POST' and form.validate():
        product_name = form.product_name.data
        product_qty = form.product_qty.data
        product_price = form.product_price.data
        app.logger.info(product_name,product_qty,product_price)
        cur = mysql.connection.cursor()
        cur.execute('INSERT INTO company_warehouse(PRODUCT_NAME,QTY,PRICE_PER_UNIT) VALUES(%s,%s,%s)',(product_name,product_qty,product_price))
        mysql.connection.commit()
        flash('Product Added !!','success')
        return redirect(url_for('warehouse'))
        cur.close()

warehouse.html

{% from 'includes/_formhelpers.html' import render_field %}
        {% include 'includes/_messages.html' %}
        <form method="POST" action="">
        <div class="modal-body">
            <div class="form-group">{{render_field(form.product_name, class_="form-control")}}</div>
            <div class="form-group">{{render_field(form.product_qty, class_="form-control")}}</div>
            <div class="form-group">{{render_field(form.product_price, class_="form-control")}}</div>
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">Add Product</button>
            <button type="reset" class="btn btn-danger">Reset</button>
        </div>
      </form>

This is the template for the above code. I had tried before by writing as I thought it was creating the problem action = "{{url_for('add_warehouse')}}" but nothing changed.

Barefaced Bear
  • 688
  • 1
  • 9
  • 30
  • You are making this much, much harder on yourself than it needs to be. I recommend that you spend an hour or two skimming the Flask Mega Tutorial (https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world") Chapters 3 and 4 in particular. – Dave W. Smith Oct 01 '19 at 04:07
  • @DaveW.Smith it will be helpfull if u can give any download link for the pdf version of the book as the current link is not showing anything – Barefaced Bear Oct 01 '19 at 04:43
  • Try https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world – Dave W. Smith Oct 01 '19 at 06:44

1 Answers1

0

I think its because you are missing the action attribute in the form.

Try this one <form method="POST" action="/add_warehouse">

Akhil Lawrence
  • 424
  • 2
  • 4
  • How can a ```input``` tag make a difference both ```button``` and ```input``` will result in same if the ```type``` is same – Barefaced Bear Oct 01 '19 at 03:37
  • `` buttons will not submit a form - they don't do anything by default. They're generally used in conjunction with JavaScript as part of an AJAX application. `` buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript. See https://stackoverflow.com/questions/290215/difference-between-input-type-button-and-input-type-submit – Akhil Lawrence Oct 01 '19 at 03:40
  • yeah i understood your part but both `````` and `````` works the same as per as i know – Barefaced Bear Oct 01 '19 at 03:43