表单值未添加到 Flask 中的数据库中

我之前发布了这个问题,但它随后被链接到一个类似的问题,该问题没有提供所需的解决方案,然后被关闭以供回答。


因此,我创建了一个 Flask 应用程序,它跟踪产品从一个位置到另一个位置的移动,同时我通过 Flask 应用程序进行移动,表单没有得到验证我尝试添加并添加到从用户那里获取输入的 html{{ form.hidden_tag() }}文件{{ form.csrf_token }}。


如果我从终端在我的命令行上运行这个应用程序,表单会被验证并被添加到数据库中,但是如果我运行 flask 应用程序并在浏览器中提交表单,它不会。


这是我的代码


class MovementForm(FlaskForm):

    to_location = SelectField('To Location', coerce=int)

    from_location = SelectField('From Location', coerce=int)

    product = SelectField('Product')

    quantity = IntegerField('Quantity')

    add_movement = SubmitField('Add Movement')


@app.route('/movements',methods=["GET","POST"])

def add_movements():

    form = MovementForm()

    form.to_location.choices = [(location.id, location.location_name) for location in Location.query.all()]

    form.from_location.choices = [(location.id, location.location_name) for location in Location.query.all()]

    form.product.choices = [(product.id, product.product_name) for product in Product.query.all()]

    form.from_location.choices.insert(0, (0, 'None'))   

    if form.validate_on_submit():

        new_movement = Movement(to_location_id=form.to_location.data, from_location_id=form.from_location.data, product_id=form.product.data, quantity=form.quantity.data)

        db.session.add(new_movement)

        db.session.commit()

        flash('Product has been moved!', 'success')

        return redirect(url_for('add_movements'))   

    return render_template('add_movements.html', form=form)



这里出了什么问题?


明月笑刀无情
浏览 93回答 1
1回答

郎朗坤

尝试在 HTML 表单中删除更改表单的操作。&nbsp;<form action="" method="post">&nbsp; &nbsp; {{ form.hidden_tag() }}&nbsp; &nbsp; {{ form.csrf_token }}&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.from_location.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.from_location(class="form-control form-control-lg") }}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.to_location.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.to_location(class="form-control form-control-lg") }}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.product.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.product(class="form-control form-control-lg") }}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.quantity.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.quantity(class="form-control form-control-lg") }}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form.add_movement(class="btn btn-outline-info") }}&nbsp; &nbsp; </div></form>&nbsp;这能解决问题吗?此外,我建议您将 Flash 消息添加到 HTML 中,因为我看到一旦提交表单,它就会返回到“add_movements”函数。因此添加:&nbsp;<div>&nbsp; &nbsp; &nbsp;{% for msg in get_flashed_messages%}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h1>{{msg}}</h1>&nbsp; &nbsp; &nbsp;{% endfor %}&nbsp;</div>&nbsp;<form action="" method="post">{{ form.hidden_tag() }}{{ form.csrf_token }}<div class="row">&nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.from_location.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; {{ form.from_location(class="form-control form-control-lg") }}&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.to_location.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; {{ form.to_location(class="form-control form-control-lg") }}&nbsp; &nbsp; </div></div><div class="row">&nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.product.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; {{ form.product(class="form-control form-control-lg") }}&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group col">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.quantity.label(class="form-control-label") }}&nbsp; &nbsp; &nbsp; &nbsp; {{ form.quantity(class="form-control form-control-lg") }}&nbsp; &nbsp; </div></div><div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.add_movement(class="btn btn-outline-info") }}</div>#编辑我注意到一旦强制丢失,产品字段中就缺少了一些东西:class MovementForm(FlaskForm):&nbsp; &nbsp; to_location = SelectField('To Location', coerce=int)&nbsp; &nbsp; from_location = SelectField('From Location', coerce=int)&nbsp; &nbsp; product = SelectField('Product', coerce=int)&nbsp; &nbsp; quantity = IntegerField('Quantity')&nbsp; &nbsp; add_movement = SubmitField('Add Movement')编辑#2如果您遇到此类问题(这种情况经常发生),我建议您也添加打印语句和 If/Else 子句。这将极大地帮助您解决问题所在(您发布的问题类型的问题是您“看不到”)并且会给您“眼睛”。例如,这就是我会做的:@app.route('/movements',methods=["GET","POST"])def add_movements():&nbsp; &nbsp; form = MovementForm()&nbsp; &nbsp; form.to_location.choices = [(location.id, location.location_name) for&nbsp;&nbsp; &nbsp; location in Location.query.all()]&nbsp; &nbsp; form.from_location.choices = [(location.id, location.location_name)&nbsp;&nbsp; &nbsp; for location in Location.query.all()]&nbsp; &nbsp; form.product.choices = [(product.id, product.product_name) for product&nbsp;&nbsp; &nbsp; in Product.query.all()]&nbsp; &nbsp; form.from_location.choices.insert(0, (0, 'None'))&nbsp;&nbsp; &nbsp; if form.validate_on_submit():&nbsp; &nbsp; &nbsp; &nbsp; print('Form Ok') #if you see the 'Form ok' to see if is validated&nbsp; &nbsp; &nbsp; &nbsp; new_movement = Movement(to_location_id=form.to_location.data,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; from_location_id=form.from_location.data,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; product_id=form.product.data, quantity=form.quantity.data)&nbsp; &nbsp; &nbsp; &nbsp; db.session.add(new_movement)&nbsp; &nbsp; &nbsp; &nbsp; db.session.commit()&nbsp; &nbsp; &nbsp; &nbsp; flash('Product has been moved!', 'success')&nbsp; &nbsp; &nbsp; &nbsp; return redirect(url_for('add_movements'))&nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print('Form Not Ok') #If you see this printed then you see that&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #is not validatedreturn render_template('add_movements.html', form=form)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python