【发布时间】:2020-09-18 14:16:26
【问题描述】:
列表看起来就像我想要的那样,有所有正确的选项,但是当我点击提交('添加运动')按钮时,它没有得到验证(它没有输入 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)
这是我的运动表模型:
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")
【问题讨论】:
标签: python flask flask-sqlalchemy flask-wtforms wtforms