使用 Flask 从 WTForms SelectField 检索键值
Retrieving a key value from WTForms SelectField using Flask
我在使用 Flask 中的 WTForms 时遇到问题,我想创建一个 add_menu 函数来将菜单添加到数据库中。用户可以相应地从 SelectField "Appetizer"、"Main Dish" 或 "Drinks" 中进行选择。因此,每当用户从 SelectField 选择值时,它都会添加到数据库中相应的 table 中。 (我使用 MySQL)。出于某种原因,当我使用 menu_type = form.menu_type.data
它给了我以下错误
mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''main_dishes'(name,ingredients,price) VALUES('Salmon', '
duude,frv
', '35')' at line 1")
It takes the right value, but I have this awkward '' signs infront of main_dishes
string
My code looks as follows:
class MenuForm(Form):
menu_type = SelectField('Menu Type', [validators.DataRequired()], choices=[('appetizers','Appetizer'),('main_dishes','Main Dish'),('desserts','Dessert'),('drinks','Drinks')], coerce=str)
name = StringField('Name', [validators.Length(min=1, max=2000)])
ingredients = TextAreaField('Ingredients', [validators.Length(min=10)])
price = DecimalField('Price (Manat)', [validators.DataRequired()])
@app.route('/add_menu', methods=['GET','POST'])
@is_logged_in
def add_menu():
form = MenuForm(request.form)
if request.method == 'POST' and form.validate():
menu_type = form.menu_type.data # <---Here is the problem
name = form.name.data
ingredients = form.ingredients.data
price = form.price.data
#Create cursor
cur = mysql.connection.cursor()
#execute
cur.execute("INSERT INTO %s(name,ingredients,price) VALUES(%s, %s, %s)", (menu_type,name,ingredients,price))
#Commit to DB
mysql.connection.commit()
#CLose connection
cur.close()
flash('Menu is Added', 'success')
return redirect(url_for('dashboard'))
return render_template('add_menu.html', form=form)
table 名称被替换为带引号的字符串,并按此执行查询。
您可能希望在绑定参数化值之前使用 table 名称构建查询。
query = "INSERT INTO {}(name,ingredients,price) VALUES(%s, %s, %s)".format(menu_type)
cur.execute(query, (name,ingredients,price))
我在使用 Flask 中的 WTForms 时遇到问题,我想创建一个 add_menu 函数来将菜单添加到数据库中。用户可以相应地从 SelectField "Appetizer"、"Main Dish" 或 "Drinks" 中进行选择。因此,每当用户从 SelectField 选择值时,它都会添加到数据库中相应的 table 中。 (我使用 MySQL)。出于某种原因,当我使用 menu_type = form.menu_type.data
它给了我以下错误
mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''main_dishes'(name,ingredients,price) VALUES('Salmon', '
duude,frv
', '35')' at line 1") It takes the right value, but I have this awkward '' signs infront ofmain_dishes
string My code looks as follows:
class MenuForm(Form):
menu_type = SelectField('Menu Type', [validators.DataRequired()], choices=[('appetizers','Appetizer'),('main_dishes','Main Dish'),('desserts','Dessert'),('drinks','Drinks')], coerce=str)
name = StringField('Name', [validators.Length(min=1, max=2000)])
ingredients = TextAreaField('Ingredients', [validators.Length(min=10)])
price = DecimalField('Price (Manat)', [validators.DataRequired()])
@app.route('/add_menu', methods=['GET','POST'])
@is_logged_in
def add_menu():
form = MenuForm(request.form)
if request.method == 'POST' and form.validate():
menu_type = form.menu_type.data # <---Here is the problem
name = form.name.data
ingredients = form.ingredients.data
price = form.price.data
#Create cursor
cur = mysql.connection.cursor()
#execute
cur.execute("INSERT INTO %s(name,ingredients,price) VALUES(%s, %s, %s)", (menu_type,name,ingredients,price))
#Commit to DB
mysql.connection.commit()
#CLose connection
cur.close()
flash('Menu is Added', 'success')
return redirect(url_for('dashboard'))
return render_template('add_menu.html', form=form)
table 名称被替换为带引号的字符串,并按此执行查询。
您可能希望在绑定参数化值之前使用 table 名称构建查询。
query = "INSERT INTO {}(name,ingredients,price) VALUES(%s, %s, %s)".format(menu_type)
cur.execute(query, (name,ingredients,price))