vendredi 2 avril 2021

The program can run but could not show the real image in Flask

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()
    os.chdir('F:\Web\Food Delivery App\static\images')
    
    for data in datas:
        image = base64.b64encode(data[3]).decode('ascii')
##        print(type(data[3]))
        
    return render_template('products.html',image=image,name=name,cuisine_type=cuisine_type,datas=datas)

products.html


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

The image cannot be shown up

As I am using wtforms to store data into database phpmyadmin and I do not know what is the best way to retrieve image data by reading their filenames using for loop data[3]? I encounter difficulties about encoding or decoding so if I want to read their image filenames and being shown on the template then what method should I use? Because the database is storing image data as BLOB property instead of reading filenames so do I first need to convert them into filenames before retrieving the image data?




Aucun commentaire:

Enregistrer un commentaire