Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Flask: How do you loop over items in a database with SQLAlchemy?

pc510

Coder
Hi,
I am trying to loop over database entries for books and display them on an e-commerce page.

The books are stored in an SQLAlchemy database with information on title, author, and price.

I want to loop through the database and display the information for each book object in the database along with a picture of the book cover. However, I am not sure how to display this information correctly.

Here is the HTML code

Code:
{% block body %}
<h1>Welcome {{ name }}</h1>

<br>
<table style="width:100%">
    <thead>
    <tr>
        <th colspan=3 class="text-center">BOOKS FOR SALE</th>
    </tr>


    <tr>
        {% for book in books %}
        <th class="text-center">{{ books.title }}</th>
        <th class="text-center">{{ books.author }}</th>
        <th class="text-center">{{ books.price }}</th>
        {% endfor %}
    </tr>
    </thead>
    <tbody>
    <tr>
        <td class="text-center"><img class="img-responsive" src="static/book1.jpg"/></td>
        <td class="text-center"><img class="img-responsive" src="static/book2.jpg"/></td>
        <td class="text-center"><img class="img-responsive" src="static/book3.jpg"/></td>
    </tr>

    </tbody>
</table>


{% endblock %}



And here is the books route leading to the page

Code:
@app.route('/books')
@login_required
def books_page():
    books = Books.query.all()
    return render_template('bookstest.html', name=current_user.username, books=books)
 
Have a look at this loop
Python:
{% for book in books %}
        <th class="text-center">{{ books.title }}</th>
        <th class="text-center">{{ books.author }}</th>
        <th class="text-center">{{ books.price }}</th>
        {% endfor %}

should be
Python:
{% for book in books %}
        <th class="text-center">{{ book.title }}</th>
        <th class="text-center">{{ book.author }}</th>
        <th class="text-center">{{ book.price }}</th>
        {% endfor %}
 
Ah, so it needs to be book.title rather than books.title. In other words, whatever you refer to the items as in the loop. Perfect. Thank you! I am still having some HTML issues, but I think it's just a matter of playing with the syntax now.
 
Have a look at this loop
Python:
{% for book in books %}
        <th class="text-center">{{ books.title }}</th>
        <th class="text-center">{{ books.author }}</th>
        <th class="text-center">{{ books.price }}</th>
        {% endfor %}

should be
Python:
{% for book in books %}
        <th class="text-center">{{ book.title }}</th>
        <th class="text-center">{{ book.author }}</th>
        <th class="text-center">{{ book.price }}</th>
        {% endfor %}
Well spotted ! But since when does HTML have loops then, or is this Python as the tag suggests ? Confused....
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom