【问题标题】: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?我正在尝试使用 wtforms 在烧瓶中创建下拉列表,但由于某种原因我的表单无法验证,有人可以告诉我为什么吗?
【发布时间】: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


    【解决方案1】:

    我想我有类似的经历,问题是表单没有针对空的选择列表进行验证。因此,在验证之前填写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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 2013-02-04
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多