I am trying to query SQLite and return image paths that meet a certain condition in a flask for loop but I am getting a broken image icon. The image_path is saved into the SQLite table which I thought was a good solution here. The code I am using is below. The first set is where I am having trouble and the second set works when the images are saved to a static
directory. Do I need to create a generator for this or I am just blundering somewhere else? This is what the SQLite table looks like,
Edit 1
I think it may help to explain what I want here. I want index.html
to show a Recent Ad section which would display recently posted ads, like on Adverts
app.py*
@app.route("/")
def index():
sqliteConnection = sqlite3.connect('finance.db')
cursor = sqliteConnection.cursor()
ads = cursor.execute("SELECT image_path FROM food WHERE active = 'Y'")
ads = cursor.fetchall()
print(ads)
return render_template("index.html", ads=ads)
This is the output for print(ads)
`[('/home/ubuntu/final/freeood/dan.jpg',), ('/home/ubuntu/final/freeood/dan3.jpg',)]'
and this next is the results for the code
for row in ads:
print(*row, sep='\t')
/home/ubuntu/final/freeood/dan.jpg
/home/ubuntu/final/freeood/dan3.jpg
index.html
{% for ad in ads %}
<img src={{ad}}>
{% endfor %}
This yields the broken icon image. I have also tried adding `ads.
I am able to display images when jpegs are saved to the static
directory with the follow code, but I am having a problem with saving to the static folder so I am trying to figure out a work around just in case.
app.py
ads = os.listdir(os.path.join(app.static_folder, "images"))
return render_template("index.html", ads=ads)
index.html
{% for ad in ads %}
<img src='/static/images/{{ad}}'/>
{% endfor %}