当我用烧瓶和打包烧瓶-mysql 编写网站时,我遇到了一个奇怪的错误。
下面是bug函数的代码:
@app.route('/calendar/editeventtitle',methods=['POST'])
def editeventtitle():
if not session.get('logged_in'):
abort(401)
try:
id = request.form.get('id',type=int)
title = request.form['title']
color = request.form['color']
delete = request.form.get('delete')
except:
pass
conn = mysql.connect()
cursor = conn.cursor()
print(id,type(id))
# try:
# print(delete,type(delete))
# except:
# pass
if id and delete:
cursor.execute('delete from events where id = %d',id)
conn.commit()
flash('Event canceled!')
return redirect(url_for('calendar'))
elif id and title and color:
cursor.execute('update events set title = %s, color = %s where id = %d',(title,color,id))
conn.commit()
flash('Event updated!')
return redirect(url_for('calendar'))
当我将四个变量发布到此页面时。我成功地得到了它们。结果print(id,type(id))
是这样的:
6 <class 'int'>
我们看到它实际上是一个整数,但是当代码开始从 db 更新或删除数据时,错误消息如下:
类型错误:%d 格式:需要一个数字,而不是 str
真的不知道原因=-=,谁能帮帮我?谢谢你。
PS:Python3.6.1、Flask 0.12.2、Flask-Mysql 1.4.0