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.