从 WTForms 解析为 MongoDB 中的数组
Parsing from WTForms into an Array in MongoDB
我正在尝试从 WTForms 解析成分列表和在 jinja 中创建食谱页面的方法,到目前为止,该应用程序从数据库中读取数据,并且可以在数组的有序列表中显示数据。我的问题是 werkzeug 在尝试提交数据时抛出错误,因此没有将表单中的任何数据推送到数据库中。
我尝试在页面本身的表单组中使用自定义表单,这使我至少可以显示包含更多成分表单的表单。我也曾尝试为此使用预构建的 WTForms 导入。
我查看了 wtforms 的文档和 mongodb 文档,但这并没有帮助澄清我的问题。
对于代码,这是我的批准
def addrecipe():
if 'username' not in session:
flash('Not possible for none members! Please create an account.')
return redirect(url_for('register'))
form = RecipeForm()
user = mongo.db.users.find_one({"name": session['username'].title()})
if request.method == 'POST' and form.validate_on_submit():
recipe = mongo.db.Recipes
recipe.insert_one({'recipe_name': request.form['recipe_name'],
'recipe_type': request.form['recipe_type'],
'recipe_desc': request.form['recipe_desc'],
'serving': request.form['serving'],
'prep_time': request.form['prep_time'],
'cook_time': request.form['cook_time'],
'ingredients': request.form['ingredients'],
'method': request.form['method'],
'img_url': request.form['image']})
flash('Recipe success!')
return redirect(url_for('index'))
return render_template('addrecipe.html', form=form)
我的数据库看起来很相似,但是成分和方法部分是数组。
表格页面在这里
class IngredientForm(FlaskForm):
description = StringField()
class MethodsForm(FlaskForm):
method = StringField()
class RecipeForm(FlaskForm):
recipe_name = StringField('Recipe Name:')
recipe_type = StringField('Recipe Type:')
recipe_desc = StringField('Description:')
serving = StringField('Serving Size:')
prep_time = StringField('Preparation Time:')
cook_time = StringField('Cooking Time:')
ingredients = FieldList(StringField(IngredientForm), 'Ingredients:', min_entries=4, max_entries=25)
method = FieldList(StringField(MethodsForm), 'Method:', min_entries=4, max_entries=10)
img_url = StringField('Got a photo link?:')
submit = SubmitField('Add Recipe')
我希望我的提交按钮可以直接将新配方推送到数据库,然后使我的模板能够立即将其构建到我的登录页面,但是 werkzeug 错误显示 400:错误请求 KEYError 'Ingredients'.
感谢任何帮助,这对我来说在后端方面有点新鲜!
所以,在像往常一样对代码进行一些挖掘和试验之后,这是我昨天没有想到的事情,因为我全神贯注于其他方式。将这些项目拆分成一个数组是一个基本的香草 python。
在我的 app.py 文件中,我修改了它以使用 .split(",") 分隔符将它们解析为数据库中的数组。
recipe.insert_one({'recipe_name': request.form['recipe_name'],
'recipe_type': request.form['recipe_type'],
'recipe_desc': request.form['recipe_desc'],
'serving': request.form['serving'],
'prep_time': request.form['prep_time'],
'cook_time': request.form['cook_time'],
Here --> 'ingredients': request.form['ingredients'].split(","),
Here --> 'method': request.form['method'].split(","),
'img_url': request.form['img_url']})
这导致它可以毫无问题地直接解析到数据库。但是限制存在的字段数量(对于那些特定的表单,这应该很容易用一些 scss 进行排序。)
已更新forms.py
class RecipeForm(FlaskForm):
recipe_name = StringField('Recipe Name:')
recipe_type = StringField('Recipe Type:')
recipe_desc = StringField('Description:')
serving = IntegerField('Serving Size:')
prep_time = IntegerField('Preparation Time:')
cook_time = IntegerField('Cooking Time:')
ingredients = StringField('Ingredients:')
method = StringField('Method:')
img_url = StringField('Got a photo link?:')
submit = SubmitField('Add Recipe')
我正在尝试从 WTForms 解析成分列表和在 jinja 中创建食谱页面的方法,到目前为止,该应用程序从数据库中读取数据,并且可以在数组的有序列表中显示数据。我的问题是 werkzeug 在尝试提交数据时抛出错误,因此没有将表单中的任何数据推送到数据库中。
我尝试在页面本身的表单组中使用自定义表单,这使我至少可以显示包含更多成分表单的表单。我也曾尝试为此使用预构建的 WTForms 导入。
我查看了 wtforms 的文档和 mongodb 文档,但这并没有帮助澄清我的问题。
对于代码,这是我的批准
def addrecipe():
if 'username' not in session:
flash('Not possible for none members! Please create an account.')
return redirect(url_for('register'))
form = RecipeForm()
user = mongo.db.users.find_one({"name": session['username'].title()})
if request.method == 'POST' and form.validate_on_submit():
recipe = mongo.db.Recipes
recipe.insert_one({'recipe_name': request.form['recipe_name'],
'recipe_type': request.form['recipe_type'],
'recipe_desc': request.form['recipe_desc'],
'serving': request.form['serving'],
'prep_time': request.form['prep_time'],
'cook_time': request.form['cook_time'],
'ingredients': request.form['ingredients'],
'method': request.form['method'],
'img_url': request.form['image']})
flash('Recipe success!')
return redirect(url_for('index'))
return render_template('addrecipe.html', form=form)
我的数据库看起来很相似,但是成分和方法部分是数组。
表格页面在这里
class IngredientForm(FlaskForm):
description = StringField()
class MethodsForm(FlaskForm):
method = StringField()
class RecipeForm(FlaskForm):
recipe_name = StringField('Recipe Name:')
recipe_type = StringField('Recipe Type:')
recipe_desc = StringField('Description:')
serving = StringField('Serving Size:')
prep_time = StringField('Preparation Time:')
cook_time = StringField('Cooking Time:')
ingredients = FieldList(StringField(IngredientForm), 'Ingredients:', min_entries=4, max_entries=25)
method = FieldList(StringField(MethodsForm), 'Method:', min_entries=4, max_entries=10)
img_url = StringField('Got a photo link?:')
submit = SubmitField('Add Recipe')
我希望我的提交按钮可以直接将新配方推送到数据库,然后使我的模板能够立即将其构建到我的登录页面,但是 werkzeug 错误显示 400:错误请求 KEYError 'Ingredients'.
感谢任何帮助,这对我来说在后端方面有点新鲜!
所以,在像往常一样对代码进行一些挖掘和试验之后,这是我昨天没有想到的事情,因为我全神贯注于其他方式。将这些项目拆分成一个数组是一个基本的香草 python。
在我的 app.py 文件中,我修改了它以使用 .split(",") 分隔符将它们解析为数据库中的数组。
recipe.insert_one({'recipe_name': request.form['recipe_name'],
'recipe_type': request.form['recipe_type'],
'recipe_desc': request.form['recipe_desc'],
'serving': request.form['serving'],
'prep_time': request.form['prep_time'],
'cook_time': request.form['cook_time'],
Here --> 'ingredients': request.form['ingredients'].split(","),
Here --> 'method': request.form['method'].split(","),
'img_url': request.form['img_url']})
这导致它可以毫无问题地直接解析到数据库。但是限制存在的字段数量(对于那些特定的表单,这应该很容易用一些 scss 进行排序。)
已更新forms.py
class RecipeForm(FlaskForm):
recipe_name = StringField('Recipe Name:')
recipe_type = StringField('Recipe Type:')
recipe_desc = StringField('Description:')
serving = IntegerField('Serving Size:')
prep_time = IntegerField('Preparation Time:')
cook_time = IntegerField('Cooking Time:')
ingredients = StringField('Ingredients:')
method = StringField('Method:')
img_url = StringField('Got a photo link?:')
submit = SubmitField('Add Recipe')