我正在尝试使用 wtforms 在烧瓶中创建下拉列表,但由于某种原因我的表单无法验证,有人可以告诉我为什么吗?
I'm trying to create drop down lists in flask using wtforms but my form won't validate for some reason, can someone tell me why?
列表看起来就像我想要的那样,有所有正确的选项,但是当我点击提交('Add Movement')按钮时,它没有得到验证(它没有进入if form.validate_on_submit()
代码块)。它曾经在我添加 SelectField 之前得到验证,所以我倾向于认为我在那里做错了什么。
@app.route('/movements', methods=['GET','POST'])
def movements():
form = MovementForm()
if form.validate_on_submit():
product = Product.query.filter_by(id=form.product.data).first()
from_location = Location.query.filter_by(id=form.from_location.data).first()
to_location = Location.query.filter_by(id=form.to_location.data).first()
m = Movement(product = product.name, from_location = from_location.name, to_location = to_location.name, quantity = form.quantity.data)
db.session.add(m)
db.session.commit()
movements = Movement.query.all()
products = Product.query.all()
locations = Location.query.all()
return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)
form.product.choices = [(product.id,product.name) for product in Product.query.all()]
form.from_location.choices = [(location.id,location.name) for location in Location.query.all()]
form.to_location.choices = [(location.id,location.name) for location in Location.query.all()]
movements = Movement.query.all()
products = Product.query.all()
locations = Location.query.all()
return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)
这是我的运动模型table:
class Movement(db.Model):
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
product = db.Column(db.String(50), nullable=False)
from_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
from_location = db.Column(db.String(50))
to_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
to_location = db.Column(db.String(50))
quantity = db.Column(db.Integer, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
这是表格:
class MovementForm(FlaskForm):
product = SelectField("Product", choices = [])
from_location = SelectField("From Location", choices = [])
to_location = SelectField("To Location", choices = [])
quantity = StringField("Quantity", validators=[DataRequired()])
add_movement = SubmitField("Add Movement")
我想我也有类似的经历,问题是表单没有针对空的选择列表进行验证。因此,在验证之前填充 form.from_location.choices
(和其他)可能会解决它:
@app.route('/movements', methods=['GET','POST'])
def movements():
form = MovementForm()
form.product.choices = [(product.id,product.name) for product in Product.query.all()]
form.from_location.choices = [(location.id,location.name) for location in Location.query.all()]
form.to_location.choices = [(location.id,location.name) for location in Location.query.all()]
if form.validate_on_submit():
# your code
# your code
return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)
列表看起来就像我想要的那样,有所有正确的选项,但是当我点击提交('Add Movement')按钮时,它没有得到验证(它没有进入if form.validate_on_submit()
代码块)。它曾经在我添加 SelectField 之前得到验证,所以我倾向于认为我在那里做错了什么。
@app.route('/movements', methods=['GET','POST'])
def movements():
form = MovementForm()
if form.validate_on_submit():
product = Product.query.filter_by(id=form.product.data).first()
from_location = Location.query.filter_by(id=form.from_location.data).first()
to_location = Location.query.filter_by(id=form.to_location.data).first()
m = Movement(product = product.name, from_location = from_location.name, to_location = to_location.name, quantity = form.quantity.data)
db.session.add(m)
db.session.commit()
movements = Movement.query.all()
products = Product.query.all()
locations = Location.query.all()
return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)
form.product.choices = [(product.id,product.name) for product in Product.query.all()]
form.from_location.choices = [(location.id,location.name) for location in Location.query.all()]
form.to_location.choices = [(location.id,location.name) for location in Location.query.all()]
movements = Movement.query.all()
products = Product.query.all()
locations = Location.query.all()
return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)
这是我的运动模型table:
class Movement(db.Model):
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
product = db.Column(db.String(50), nullable=False)
from_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
from_location = db.Column(db.String(50))
to_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
to_location = db.Column(db.String(50))
quantity = db.Column(db.Integer, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
这是表格:
class MovementForm(FlaskForm):
product = SelectField("Product", choices = [])
from_location = SelectField("From Location", choices = [])
to_location = SelectField("To Location", choices = [])
quantity = StringField("Quantity", validators=[DataRequired()])
add_movement = SubmitField("Add Movement")
我想我也有类似的经历,问题是表单没有针对空的选择列表进行验证。因此,在验证之前填充 form.from_location.choices
(和其他)可能会解决它:
@app.route('/movements', methods=['GET','POST'])
def movements():
form = MovementForm()
form.product.choices = [(product.id,product.name) for product in Product.query.all()]
form.from_location.choices = [(location.id,location.name) for location in Location.query.all()]
form.to_location.choices = [(location.id,location.name) for location in Location.query.all()]
if form.validate_on_submit():
# your code
# your code
return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)