0

I am trying to create an online book store. I really can't figure out what's wrong in this code. It shows the following error:

werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'roomno'

@app.route('/order_book',methods=['GET','POST'])
def order_book():
    if not session.get('logged_in'):
        return render_template('user_login.html')
    user_id=session['userid']
    cur=connection.cursor()
    cur.execute("SELECT * FROM address WHERE user_id=%s",[user_id])
    addr_data=cur.fetchone()
    if request.method == 'POST':
        book_id = request.form['book_id1']
    if not addr_data:
        if request.method == 'POST':
            room_no = request.form['roomno']
            building = request.form['building']
            street = request.form['street']
            city = request.form['city']
            zipcode = request.form['zipcode']
            cur.execute("INSERT INTO address(user_id,room_no,building,street,city,zipcode) VALUES(%s,%s,%s,%s,%s,%s)",(userid,room_no,building,street,city,zipcode))
            connection.commit()
            print('Address Added')
            return redirect(url_for('order_book'))
        return render_template('address.html')
    cur.execute("SELECT * FROM books WHERE book_id=%s",[book_id])
    book_data=cur.fetchone()
    cur.execute("SELECT * FROM login WHERE user_id=%s",[user_id])
    cust_data=cur.fetchone()
    cur.close()
    print(cust_data)
    print(addr_data)
    print(book_data)
    return render_template('order_book.html',addr_data=addr_data,book_data=book_data,cust_data=cust_data)

html code

order.html

<html>  
<h1>PLACE ORDER</h1><br>



            <h5><a class="card-title" id="" href="">{{ books_data[0] }}</a></h5>
            <h4>{{ books_data[1] }}</h4>
            <span>{{ books_data[2] }}</span>
        

<form action="placeorder" method="POST">
  <div class="nav-link">
  <div class="form-group">
    Room no<br><input type="text" name="roomno"  value={{ addr_data[2] }} disabled><br><br>
    building<br><input type="text" name="building"  value={{ addr_data[3] }} disabled><br><br>
    street<br><input type="text" name="street"  value={{ addr_data[4] }} disabled><br><br>
    city<br><input type="text" name="city"  value={{ addr_data[5] }} disabled><br><br>
    zipcode<br><input type="text" name="zipcode"  value={{ addr_data[6] }} disabled><br><br>
    Mobilenumber<br><input type="text" name="phoneno"  value={{ cust_data[4] }} disabled><br><br>
    Email<br><input type="text" name="emailids"  value={{ cust_data[3] }} disabled><br><br>
<input type="text" name="book_id"  value={{ book_data[1] }} style="display:none">
<input type="text" name="user_id"  value={{ cust_data[0] }} style="display:none">
 <input type="text" name="book_price"  value={{ book_data[8] }} style="display:none">
  <input type="submit" class="btn btn-primary" value="Place Order">
</form>
</div>
</div>
</html>

I have tried to change the html page and I have tried db changes. I don't know what to do.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ChocoSanju
  • 35
  • 6

1 Answers1

0

The issue is your input fields are marked as disabled. Disabled inputs will not be POSTed. See Values of disabled inputs will not be submitted for more information. A few workarounds are to instead use readonly="readonly" or <input type="hidden">, depending on if you want users to see the inputs or not. You could also remove the disabled tag via JavaScript on submission of the form. Doing any of those things should result in your input values being POSTed correctly and the your Flask backend should then receive the data.

h0r53
  • 3,034
  • 2
  • 16
  • 25