-2

I have implemented a Flask APP where I can upload a set of images, store the image in a folder, store in sqlite db the relative path for each image and then show the uploaded images in the homepage. This is the code:

app.py

   @app.route('/') 
   def home():
    conn=db_connection()


    cursor = conn.execute("SELECT * from upload")
    items=cursor.fetchall()
    filename=row[4], path file=row[5] ,cla=row[6], 
    entities=row[7], pathentities=row[8])


return render_template('/dist/home.html', items=items)
    




    @app.route('/uploader', methods = ["POST"])
    def upload_file():

     if request.method == 'POST':
     conn = db_connection()
     cursor = conn.cursor()
     uploaded_files = request.files.getlist("file[]")

     UPLOAD_FOLDER =r"./Images"
     app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
     for file in uploaded_files:
            filename=file.filename
            path=os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
            pathfilename=path
            file.save (file)

The images are correctly stored.

in home.html to present the whole set of images, from DB set, I have:

<tbody>
{% for item in items %}
<tr><td>
<a > {{item[1]}} </a> </td> 
<td> {{item[3]}} </td> 
<td> {{item[4]}} </td> 
<td> <a> {{item[5]}} </a> 
</td> </tr> 
{% endfor %}

Now I would like to clink on the link in the table and open the associated image. Hos should I do in flask?

charchit
  • 1,492
  • 2
  • 6
  • 17
user3043636
  • 559
  • 6
  • 23

1 Answers1

1

You should store the images in static folder in root directory and save the name of image in the database.

Then you can access the image using url_for

{% for item in items %}
<a href="{{url_for("static",filename=item[1])}}"></a>
{% endfor %}
charchit
  • 1,492
  • 2
  • 6
  • 17