首页 > 解决方案 > 从 WTForms 解析成 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: bad request KEYError 'Ingredients'。

我很感激这方面的任何帮助,这对我来说在后端有点新鲜!

标签: pythonmongodbjinja2flask-wtformswerkzeug

解决方案


因此,在像往常一样对代码进行了一些挖掘和玩弄之后,这是我昨天实际上没有想到的事情,因为我全神贯注于其他方式来做这件事。将这些项目拆分为数组是一种基本的香草 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')

推荐阅读