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

Form Values are not getting added to the database in Flask

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

所以我创建了一个 Flask 应用程序来跟踪产品从一个位置到另一个位置的移动,同时我通过 Flask 应用程序进行移动,但表格没有得到验证我尝试添加 {{ form.hidden_tag() }}{{ form.csrf_token }} 到接受用户输入的 html 文件。

如果我 运行 从我的命令行终端上的这个应用程序,表单将被验证并被添加到数据库中,但是如果我 运行 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)

这是我的 html 文件

 <form action="/movements" method="post">
        {{ form.hidden_tag() }}
        {{ form.csrf_token }}
        <div class="row">
            <div class="form-group col">
                {{ form.from_location.label(class="form-control-label") }}
                {{ form.from_location(class="form-control form-control-lg") }}
            </div>
            <div class="form-group col">
                {{ form.to_location.label(class="form-control-label") }}
                {{ form.to_location(class="form-control form-control-lg") }}
            </div>
        </div>
        <div class="row">
            <div class="form-group col">
                {{ form.product.label(class="form-control-label") }}
                {{ form.product(class="form-control form-control-lg") }}
            </div>
            <div class="form-group col">
                {{ form.quantity.label(class="form-control-label") }}
                {{ form.quantity(class="form-control form-control-lg") }}
            </div>
        </div>
        <div class="form-group">
                {{ form.add_movement(class="btn btn-outline-info") }}
        </div>
    </form> 

这是怎么回事?

尝试删除以更改 HTML 表单中的表单操作。

 <form action="" method="post">
    {{ form.hidden_tag() }}
    {{ form.csrf_token }}
    <div class="row">
        <div class="form-group col">
            {{ form.from_location.label(class="form-control-label") }}
            {{ form.from_location(class="form-control form-control-lg") }}
        </div>
        <div class="form-group col">
            {{ form.to_location.label(class="form-control-label") }}
            {{ form.to_location(class="form-control form-control-lg") }}
        </div>
    </div>
    <div class="row">
        <div class="form-group col">
            {{ form.product.label(class="form-control-label") }}
            {{ form.product(class="form-control form-control-lg") }}
        </div>
        <div class="form-group col">
            {{ form.quantity.label(class="form-control-label") }}
            {{ form.quantity(class="form-control form-control-lg") }}
        </div>
    </div>
    <div class="form-group">
            {{ form.add_movement(class="btn btn-outline-info") }}
    </div>
</form> 

这是否解决了问题?

此外,我建议您将 Flash 消息添加到 HTML,因为我看到一旦表单被提交,它 returns 返回到 'add_movements' 函数。因此添加:

 <div>
     {% for msg in get_flashed_messages%}
         <h1>{{msg}}</h1>
     {% endfor %}
 </div>
 <form action="" method="post">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
<div class="row">
    <div class="form-group col">
        {{ form.from_location.label(class="form-control-label") }}
        {{ form.from_location(class="form-control form-control-lg") }}
    </div>
    <div class="form-group col">
        {{ form.to_location.label(class="form-control-label") }}
        {{ form.to_location(class="form-control form-control-lg") }}
    </div>
</div>
<div class="row">
    <div class="form-group col">
        {{ form.product.label(class="form-control-label") }}
        {{ form.product(class="form-control form-control-lg") }}
    </div>
    <div class="form-group col">
        {{ form.quantity.label(class="form-control-label") }}
        {{ form.quantity(class="form-control form-control-lg") }}
    </div>
</div>
<div class="form-group">
        {{ form.add_movement(class="btn btn-outline-info") }}
</div>

#EDIT

强制输入后,我注意到产品字段中缺少某些内容:

class MovementForm(FlaskForm):
    to_location = SelectField('To Location', coerce=int)
    from_location = SelectField('From Location', coerce=int)
    product = SelectField('Product', coerce=int)
    quantity = IntegerField('Quantity')
    add_movement = SubmitField('Add Movement')

EDIT #2


如果您 运行 遇到此类问题(这种情况经常发生),我建议您也添加打印语句和 If/Else 子句。这将极大地帮助您解决问题(您发布的问题类型的问题是您“看不到”)并且会给您 'eyes.'

例如,这就是我会做的:

@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():
        print('Form Ok') #if you see the 'Form ok' to see if is validated
        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'))
     else:
         print('Form Not Ok') #If you see this printed then you see that 
          #is not validated

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