main.py
@app.route('/products/<name>')
def products(name):
cursor = mysql.connection.cursor()
query = 'SELECT duty FROM staff WHERE name = %s'
cursor.execute(query,(name,))
cuisine_type = cursor.fetchone()[0]
query = 'SELECT * from cuisines WHERE type = %s'
cursor.execute(query,(cuisine_type,))
datas = cursor.fetchall()
for data in datas:
image_file = data[3]
image_real = base64.decodebytes(data[3])
return render_template('products.html',image_real=image_real,name=name,cuisine_type=cuisine_type,datas=datas)
@app.route('/product_edit/<name>',methods=['GET','POST'])
def product_edit(name):
cursor = mysql.connection.cursor()
query = 'SELECT duty FROM staff WHERE name = %s'
cursor.execute(query,(name,))
cuisine_type = cursor.fetchone()[0]
form = Products(CombinedMultiDict((request.files, request.form)))
if form.validate_on_submit():
item = form.item.data
price = form.price.data
f = form.image.data
filename = secure_filename(f.filename)
if os.path.exists('photos') == False:
os.mkdir('photos')
else:
pass
f.save(os.path.join(
UPLOAD_FOLDER,filename
))
cursor = mysql.connection.cursor()
query = 'INSERT INTO cuisines(type,item,price,img) VALUES(%s,%s,%s,%s)'
values = (cuisine_type,item,price,filename)
cursor.execute(query,values)
mysql.connection.commit()
cursor.close()
## return 'Success'
return redirect(url_for('products',name=name,cuisine_type=cuisine_type))
return render_template('product_edit.html',cuisine_type=cuisine_type,name=name,form=form)
models.py
class Products(FlaskForm):
item = StringField('Item',[validators.Length(max=100)])
price = DecimalField('Price per unit', validators=[validators.Optional()], places=1)
image = FileField('image',validators=[FileRequired()])
submit = SubmitField('Add')
products.html
My target is to show the image that stored in the database phpmyAdmin on the table of html template by using data[3]
to represent. However when I run the program the image could not show up. How can I convert the blob property into jpg or png so the image could be shown on the html webpage? Is there any methods to represent the image filename by using for loop data
? I would like to show the specific images accordingly from the database
Aucun commentaire:
Enregistrer un commentaire