Search code examples
pythonflaskjinja2

Returning a list using flask and SQLite


I was wondering how I could return individual values from an SQLite table into a list using flask. At the moment the result is showing all three available values from the table but I want them to be listed individually (green text). Below I print the values in the code to show you what I would like reflected on the webpage. The end goal is for each to be a link to the respective ad. Frankly I am pretty new and am stuck so any direction would be super helpful on what the next steps should be here. The links to the respective ads will be done with dynamic URLs which I believe I know how to do. Right now, I just want to know if I could get the values from the flask template.

enter image description here

app.py

@app.route("/my_ads")
@login_required
def my_ads():

conn = sqlite3.connect('food.db')
conn.row_factory = lambda cursor, row: row[0]
c = conn.cursor()
ads = c.execute("SELECT image_key FROM food").fetchall()
for row in ads:
     print(row)

return render_template("my_ads.html", ads=ads

my_ads.html

{% extends "layout.html" %}

{% block main %}
...
<div class="row">
   <div class="column">
    <h2>My Ads</h2>

    <ul>
        {% for ad in ads %}
        <li>{{ads}}</li>
        {% endfor %}
    </ul>
 </div>
 {% endblock %}

Solution

  • In your <ul>'s for loop, instead of using <li>{{ads}}</li>, use just <li>{{ad}}</li> to access the individual values.