I'm having a strange bug when I write a website with flask and package flask-mysql.
Here is the code of the bug function:
@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'))
When I post the four variables to this page. I succesfully get them. And the result of print(id,type(id))
is like:
6 <class 'int'>
We see it's really an integer, but when the code starts to update or delete data from db, here is the error message:
TypeError: %d format: a number is required, not str
Really don't know the reason =-=, anyone can help me? Thank you.
PS: Python3.6.1, Flask 0.12.2, Flask-Mysql 1.4.0