Python 表单值未添加到Flask中的数据库
我早些时候发布了这个问题,但随后它被链接到一个类似的问题,该问题没有提供所需的解决方案,然后被关闭以供回答 因此,我创建了一个Flask应用程序,它跟踪产品从一个位置到另一个位置的移动,同时我通过Flask应用程序进行移动。表单没有得到验证。我尝试将Python 表单值未添加到Flask中的数据库,python,flask,flask-wtforms,Python,Flask,Flask Wtforms,我早些时候发布了这个问题,但随后它被链接到一个类似的问题,该问题没有提供所需的解决方案,然后被关闭以供回答 因此,我创建了一个Flask应用程序,它跟踪产品从一个位置到另一个位置的移动,同时我通过Flask应用程序进行移动。表单没有得到验证。我尝试将{{form.hidden_tag()}和{form.csrf_token}添加到接收用户输入的html文件中 如果我从命令行上的终端运行此应用程序,则表单将被验证并添加到数据库中,但如果我运行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>
{{form.hidden_tag()}}
{{form.csrf_token}
{{form.from_location.label(class=“form control label”)}
{{form.from_location(class=“form control form control lg”)}
{{form.to_location.label(class=“form control label”)}
{{form.to_location(class=“form control form control lg”)}
{{form.product.label(class=“form control label”)}
{{form.product(class=“form control form control lg”)}
{{form.quantity.label(class=“form control label”)}
{{form.quantity(class=“form control form control lg”)}
{{form.add_movement(class=“btn btn outline info”)}
这里出了什么问题?尝试删除以更改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>
编辑#2
如果您遇到此类问题(这种情况经常发生),我建议您添加一个print语句和一个If/Else子句。这将极大地帮助你找到问题所在(你发布的问题类型是你“看不见”),并会给你“眼睛” 例如,我会这样做:
@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)
不,它没有,值仍然没有反映在数据库中。如果添加此flash消息,您可以在浏览器上看到flash消息吗?产品字段中的“强制”参数起作用。非常感谢。我认为问题在于表单返回false,因此在'if form.validate_on_submit():'之后的所有内容都返回false,因为它从不运行
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')
@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)