[flask_sqlalchemy ]插入数据时发生错误后如何处理

 

示例代码:

def add_supplier():
    form = request.form
    if request.method == 'POST':
        print(form)
        supplier = Supplier(form['supplier_name'],
                            form['supplier_constract'],
                            form['supplier_phone'],
                            form['supplier_address'],
                            form['supplier_remark'])
        try:
            db.session.add(supplier)
            db.session.commit()
            flash('供应商添加成功!', 'success')
        except:
            db.session.rollback()
            flash('输入不合法,请重新输入!', 'danger')
    return redirect(url_for('supplier.list_supplier'))

参考文档:

flask_sqlalchemy 插入数据时发生错误后如何处理?

 

posted @ 2019-12-01 17:24  wangju003  阅读(912)  评论(0编辑  收藏  举报