【问题标题】:Error binding parameter '3' using wtforms, mysqlalchemy with Flask使用 wtforms、mysqlalchemy 和 Flask 绑定参数“3”时出错
【发布时间】:2021-06-21 12:47:43
【问题描述】:

Flask 新手,这是我在这里的第一篇文章!处理带有返回“绑定参数“3”错误的 SelectMultipleField 条目的表单条目。

在forms.py中:

class MultiCheckboxField(SelectMultipleField):
    widget = widgets.ListWidget(prefix_label=False)
    option_widget = widgets.CheckboxInput()

class SWProductForm(FlaskForm):
    string_of_files = [
    'genomics', 'mapping', 'identification', 'analytics', 'statistics', 'NDVI', 'imagery', 'testing', 'marketing', 'supplychain', 'irrigation'
    ]
    files = [(x, x) for x in string_of_files]

sw_categories = MultiCheckboxField(
        'Category', 
        choices=files,
        validators=[DataRequired()] 
    )

在models.py中:

class SWProducts(db.Model):
    sw_categories = db.Column(
        db.String,
        index=False,
        unique=False,
        nullable=False
    )

   def __init__(self, sw_categories):
        self.sw_categories = sw_categories

在 routes.py 中:

@home_bp.route('/ag-software-entry', methods=['GET', 'POST'])
def ag_software_entry():
    form = SWProductForm()
if request.method == 'POST':
        if form.is_submitted():
            print ("submitted")
        if form.validate():
            print ("valid")
        print(form.errors)
        if form.validate_on_submit():
            # Get Form Fields
            sw_categories = form.sw_categories.data

            sw_products = SWProducts(
                sw_categories = form.sw_categories.data,             
                )

            db.session.add(sw_products)
            db.session.commit()
            flash('Record was successfully added')

            return render_template(
                'software_entry.html',
                sw_products=sw_products
                )
        else:
            print("bad")
    return render_template('add_new_agtech_sw.html', form=form)

html/神社:

  <fieldset class="form-field">
      {{ form.sw_categories.label }}
      <br />
      {{ form.sw_categories }}
      {% if form.sw_categories.errors %}
      <ul class="errors">
        {% for error in form.sw_categories.errors %}
          <li>{{ error }}</li>
        {% endfor %}
      </ul>
      {% endif %}
    </fieldset>

完整的错误是:sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 3 - 可能是不支持的类型。 [SQL: INSERT INTO sw_products (sw_company_name, sw_company_product, sw_software_components, sw_categories, sw_product_description, sw_product_img, sw_os_license, sw_references, sw_locations_desc, sw_locations_img) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [参数:('软件公司','软件产品','软件元素',['映射','统计','NDVI'],'产品描述','Screen_Shot_2021-03-20_at_11.35.51_AM.png' , '操作系统许可证', '软件参考', '软件位置', 'Screen_Shot_2021-03-20_at_11.35.51_AM.png')] (此错误的背景:http://sqlalche.me/e/13/rvf5

在添加复选框选项之前,我删除了其他正在工作的字段条目。

【问题讨论】:

    标签: python flask flask-sqlalchemy wtforms


    【解决方案1】:

    好的,所以我找到了解决方案。这是在路由中使用join方法的问题:

       sw_categories = ', '.join(form.sw_categories.data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2015-08-04
      • 1970-01-01
      • 2012-08-23
      相关资源
      最近更新 更多