while adding the image in SQLAlchemy database, getting below error-
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) Error binding parameter 9: type ‘FileStorage’ is not supported
[SQL: INSERT INTO addproduct (name, price, discount, stock, colors, description, category_id, brand_id, product_picture) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: (‘Iphone15’, 60000, 0, 45, ‘Black’, ‘Mobile’, 1, 1, <FileStorage: ‘mobile.jpg’ (‘image/jpeg’)>)]
Please help me to fix this error.
<code>class AddProducts(FlaskForm):
name = StringField('Name',validators=[DataRequired()])
price = IntegerField('Price',validators=[DataRequired()])
product_picture = FileField('Product Picture', validators=[FileRequired()])
Submit = SubmitField('Add Product')
class Addproduct(db.Model):
__tablename__ = 'addproduct'
id:Mapped[int] = mapped_column(Integer,primary_key=True)
name:Mapped[str] = mapped_column(String(80),nullable=False, unique=True)
price:Mapped[int] = mapped_column(Integer,nullable=False)
brand_id:Mapped[int]= mapped_column(Integer, db.ForeignKey('brand.id'),nullable=False)
brand:Mapped['Brand'] = db.relationship(back_populates='product')
product_picture:Mapped[str] = mapped_column(String(1000), nullable=False)
class Brand(db.Model):
__tablename__ = 'brand'
id:Mapped[int] = mapped_column(Integer,primary_key=True)
name:Mapped[str] = mapped_column(String(30),nullable=False, unique=True)
product:Mapped[List['Addproduct']] = db.relationship(back_populates='brand')
This is how I am trying to add the photo of the product:
@app.route('/addproduct', methods=['POST','GET'])
def addproduct():
form = AddProducts()
if form.validate_on_submit():
name = form.name.data
price = form.price.data
file = form.product_picture.data
file_name = secure_filename(file.filename)
file_path = f'static/images/{file_name}'
file.save(file_path)
product = Addproduct(name=name, price=price, product_picture=file_path)
form.populate_obj(product)
db.session.add(product)
db.session.commit()
flash(f'The product {name} has been added.')
return render_template('products/addproduct.html', form=form)
addproduct.html:
{% extends 'layout.html' %}
{% include 'messages.html' %}
{% from 'bootstrap5/form.html' import render_form %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class = "text-center h1">Add a Product</div>
{{render_form(form, novalidate=True)}}
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
{% endblock %}
</code>
<code>class AddProducts(FlaskForm):
name = StringField('Name',validators=[DataRequired()])
price = IntegerField('Price',validators=[DataRequired()])
product_picture = FileField('Product Picture', validators=[FileRequired()])
Submit = SubmitField('Add Product')
class Addproduct(db.Model):
__tablename__ = 'addproduct'
id:Mapped[int] = mapped_column(Integer,primary_key=True)
name:Mapped[str] = mapped_column(String(80),nullable=False, unique=True)
price:Mapped[int] = mapped_column(Integer,nullable=False)
brand_id:Mapped[int]= mapped_column(Integer, db.ForeignKey('brand.id'),nullable=False)
brand:Mapped['Brand'] = db.relationship(back_populates='product')
product_picture:Mapped[str] = mapped_column(String(1000), nullable=False)
class Brand(db.Model):
__tablename__ = 'brand'
id:Mapped[int] = mapped_column(Integer,primary_key=True)
name:Mapped[str] = mapped_column(String(30),nullable=False, unique=True)
product:Mapped[List['Addproduct']] = db.relationship(back_populates='brand')
This is how I am trying to add the photo of the product:
@app.route('/addproduct', methods=['POST','GET'])
def addproduct():
form = AddProducts()
if form.validate_on_submit():
name = form.name.data
price = form.price.data
file = form.product_picture.data
file_name = secure_filename(file.filename)
file_path = f'static/images/{file_name}'
file.save(file_path)
product = Addproduct(name=name, price=price, product_picture=file_path)
form.populate_obj(product)
db.session.add(product)
db.session.commit()
flash(f'The product {name} has been added.')
return render_template('products/addproduct.html', form=form)
addproduct.html:
{% extends 'layout.html' %}
{% include 'messages.html' %}
{% from 'bootstrap5/form.html' import render_form %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class = "text-center h1">Add a Product</div>
{{render_form(form, novalidate=True)}}
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
{% endblock %}
</code>
class AddProducts(FlaskForm):
name = StringField('Name',validators=[DataRequired()])
price = IntegerField('Price',validators=[DataRequired()])
product_picture = FileField('Product Picture', validators=[FileRequired()])
Submit = SubmitField('Add Product')
class Addproduct(db.Model):
__tablename__ = 'addproduct'
id:Mapped[int] = mapped_column(Integer,primary_key=True)
name:Mapped[str] = mapped_column(String(80),nullable=False, unique=True)
price:Mapped[int] = mapped_column(Integer,nullable=False)
brand_id:Mapped[int]= mapped_column(Integer, db.ForeignKey('brand.id'),nullable=False)
brand:Mapped['Brand'] = db.relationship(back_populates='product')
product_picture:Mapped[str] = mapped_column(String(1000), nullable=False)
class Brand(db.Model):
__tablename__ = 'brand'
id:Mapped[int] = mapped_column(Integer,primary_key=True)
name:Mapped[str] = mapped_column(String(30),nullable=False, unique=True)
product:Mapped[List['Addproduct']] = db.relationship(back_populates='brand')
This is how I am trying to add the photo of the product:
@app.route('/addproduct', methods=['POST','GET'])
def addproduct():
form = AddProducts()
if form.validate_on_submit():
name = form.name.data
price = form.price.data
file = form.product_picture.data
file_name = secure_filename(file.filename)
file_path = f'static/images/{file_name}'
file.save(file_path)
product = Addproduct(name=name, price=price, product_picture=file_path)
form.populate_obj(product)
db.session.add(product)
db.session.commit()
flash(f'The product {name} has been added.')
return render_template('products/addproduct.html', form=form)
addproduct.html:
{% extends 'layout.html' %}
{% include 'messages.html' %}
{% from 'bootstrap5/form.html' import render_form %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class = "text-center h1">Add a Product</div>
{{render_form(form, novalidate=True)}}
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
{% endblock %}