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
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)