Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on a python web application and i keep getting the type error.STORAGE FILE IS SUPPOSED TO BE WERKEUG FORMAT something like that.Here's my code

What I have tried:

MODELS.PY
Python
from shop import db
import datetime

class Addproduct(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    price = db.Column(db.Numeric(10,2),nullable=False)
    discount = db.Column(db.Integer,default=0)
    stock = db.Column(db.Integer,nullable=False)
    colors = db.Column(db.Text,nullable=False)
    desc = db.Column(db.Text,nullable=False)
    pub_date = db.Column(db.DateTime,nullable=False)

    brand_id = db.Column(db.Integer,db.ForeignKey('brand.id'),nullable=False)
    brand = db.relationship('Brand',backref=db.backref('brand',lazy=True))

    category_id = db.Column(db.Integer,db.ForeignKey('category.id'),nullable=False)
    category = db.relationship('Category',backref=db.backref('posts',lazy=True))

    image_1 = db.Column(db.String(150),nullable=False,default='image.jpg')
    image_2 = db.Column(db.String(150),nullable=False,default='image.jpg')
    image_3 = db.Column(db.String(150),nullable=False,default='image.jpg')

    def __repr__(self):
        return '<addproduct %r="">' % self.name



class Brand(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), nullable=False, unique=True)

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), nullable=False, unique=True)

db.create_all()



ROOTS.PY
from flask import redirect, render_template, url_for, flash, request
from shop import db, app, photos
from .models import Brand, Category
from .forms import Addproduct

@app.route('/addbrand', methods=['GET','POST'])
def addbrand():
    if request.method=="POST":
        getbrand = request.form.get('brand')
        brand = Brand(name=getbrand)
        db.session.add(brand)
        flash(f'The Brand {getbrand} was added to your database','success')
        db.session.commit()
        return redirect(url_for('addbrand'))
    return render_template('products/addbrand.html', brands='brands')

@app.route('/addcat', methods=['GET','POST'])
def addcat():
    if request.method=="POST":
        getbrand = request.form.get('category')
        cat = Category(name=getbrand)
        db.session.add(cat)
        flash(f'The Category {getbrand} was added to your database','success')
        db.session.commit()
        return redirect(url_for('addcat'))
    return render_template('products/addbrand.html')

@app.route('/Addproduct', methods=['GET','POST'])
def addproduct():
    brands = Brand.query.all()
    categories = Category.query.all()
    form = Addproduct(request.form)
    if request.method == "POST":
        photos.save(request.files.get('images_1'))
        photos.save(request.files.get('images_2'))
        photos.save(request.files.get('images_3'))
    return render_template('products/addproduct.html', title="Add Product page", form= form,brands=brands,categories=categories)
Posted
Updated 13-Sep-20 23:42pm
v2

Quote:
i keep getting the type error.STORAGE FILE IS SUPPOSED TO BE WERKEUG FORMAT something like that.
"Something like that" is not any use. Please show the exact error message and which line of code causes it.
 
Share this answer
 
Potentially the error is in addproduct method at one of the photos.save

Similar to python - TypeError: storage must be a werkzeug.FileStorage in Flask Upload - Stack Overflow[^], would suggest you to make sure there is some file to upload.

With debugging, you should be able to see what is the value being passed for upload/save to DB.
 
Share this answer
 
Comments
Member 12401110 15-Sep-20 2:02am    
thank for your assistance
Sandeep Mewara 15-Sep-20 2:31am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900