gpt4 book ai didi

python - 400 错误请求 : KeyError:

转载 作者:行者123 更新时间:2023-11-29 15:44:01 26 4
gpt4 key购买 nike

我正在尝试创建一家在线书店。我真的不明白这段代码有什么问题。它显示以下错误:

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代码

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>

我尝试过更改 html 页面,并且尝试过更改数据库。我不知道该怎么办。

最佳答案

问题是您的输入字段被标记为禁用。禁用的输入不会被发布。请参阅Values of disabled inputs will not be submitted了解更多信息。一些解决方法是改用 readonly="readonly"<input type="hidden"> ,取决于您是否希望用户看到输入。您还可以在提交表单时通过 JavaScript 删除禁用的标签。执行任何这些操作都应该会导致您的输入值被正确发布,然后您的 Flask 后端应该会收到数据。

关于python - 400 错误请求 : KeyError:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57271658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com