You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 %}
Add a Product
{{render_form(form, novalidate=True)}}
{% endblock %}
Guidelines
I have read and understood this category's guidelines before making this post.
Programming HelpProgramming languages, open source, and software development.
1 participant
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Body
Hi,
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.
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 %}
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions