0

When I hit the submit button I get a 400 Bad Request error. It actually redirects to the '/' page, but it displays the error instead of the page.

These two buttons are on the same page, but only the 'Add' button works.

<div class="col-lg-2 col-md-2">
    <form role="form" method="POST" action="/">
      <button type="submit" class="btn btn-success-register">Register</button>
    </form>
</div>

<div class="col-lg-2 col-md-2">
      <form action="/add">
        <button type="submit" class="btn btn-success-add">Add</button>
      </form><br class="visible-xs visible-sm">
</div>

Root Route:

@app.route('/', methods=['GET', 'POST'])
def index():

    if request.method == "POST":
        if request.form['items'] == 'items':
            return redirect(url_for('data'))

    return render_template('register.html')

Route that the buttons are on:

@app.route('/data', methods=['GET','POST'])
def data():

    deleted_items_list = []

    if request.method == "POST":
        if request.form['delete'] == 'delete':
            deleted_items = request.form.getlist('delete-check')
            for item in deleted_items:
                deleted_items_list.append(item.encode("utf-8"))
            for item in deleted_items_list:

                g.db = connect_db()

                g.db.execute('DELETE FROM yardsale WHERE Description = ?', (item,))

                g.db.commit()
                g.db.close()
                print("items were deleted")

    items = ["id"]
    g.db = connect_db()
    c = g.db.execute('select * from yardsale')
    items = [dict(id=row[0], description=row[1], seller=row[2], SP=row[3], MSP=row[4], SF=row[5], Notes=row[6]) for row in c.fetchall()]
    g.db.close()

    return render_template('data.html', items=items)
Scott
  • 471
  • 3
  • 9
  • 19
  • 2
    It might similar to the followings. Confirm this. http://stackoverflow.com/questions/8552675/form-sending-error-flask http://stackoverflow.com/questions/19578613/posting-data-on-flask-via-form-is-giving-400-bad-request – harukaeru Aug 26 '15 at 00:01
  • Show us the code from your `/` route, that will show why the form submission doesn't work. – Matt Healy Aug 26 '15 at 00:31
  • Sorry. Should have posted that originally. – Scott Aug 26 '15 at 00:39

2 Answers2

0

You are trying to access request.form['items'] but this isn't defined anywhere in your form. You're most likely raising a KeyError with this code.

For this code to work you'll need to ensure you have an in your form with the name items.

Alternatively, if you want the form submission to work even without the items input being present, you can change your code to something like this

    if request.form.get('items') == 'items':

As this won't raise a KeyError if the items input is not defined.

Matt Healy
  • 18,033
  • 4
  • 56
  • 56
  • I could be misunderstanding, but I do not think this is my problem. The Register button (the one i'm having the issue with) is on the '/data' page not the '/' page. The 'items' form is for a button that is working on the '/' page. – Scott Aug 26 '15 at 00:49
  • Your form around the Register button currently has `action="/"` which means your `index` route will handle the submission of that form, not the `data` route. – Matt Healy Aug 26 '15 at 00:52
-1

I think you're missing name attribute.

MadRabbit
  • 2,460
  • 2
  • 17
  • 18