I have book template which display more info about the book like title,price and author etc..
in the same template I've similar books section where I want to display similar book to the featured book according to the classification (both have the same classification)
and to do that i'm thinking of shuffling books i retrieve from the database instead of just using normal loop to make it more dynamic as in every book has that classification, the page shows different books not the same books every time
here's the code in views.py
def book(request, book_id):
book = get_object_or_404(Book, pk=book_id)
similar_books = Book.objects.all()[:4]
book_context = {
'book': book,
'similar_books': similar_books
}
return render(request, 'books/book.html', book_context)
and this the the code in my template
<div class="row">
<div class="col py-5 text-center">
<h3 class="mb-5">Similar books</h3>
<div class="row d-flex justify-content-center">
{% for similar_book in similar_books %}
{% if similar_book.classification == book.classification and similar_book.id > book.id %}
<div class="col-md-3">
<a href="{% url 'book' similar_book.id %}"><img src="{{ similar_book.img.url }}"></a>
<a href="{% url 'book' similar_book.id %}"><p class="mt-2">{{ similar_book.title}}</p></a>
<p class="text-muted">{{ similar_book.author }}</p>
<p>{{ similar_book.price }}</p>
</div>
{% endif %}
{% endfor %}
</div>
</div>
the reason i'm including similar_book.id > book.id
is that i don't want it to display the featured book in its similar books
i know it's not the best way to do it with logical as if the featured book is the last book in the list, the loop will stop
i also thought of replacing the if statement above with
{% for similar_book in similar_books[{{ similar_book.id }} -1:] %}
to start looping through books after the featured book and adding if statement {% if book.id == len(similar_books) %}
then {% similar_book.id == 1 %}
but not sure if it's correct logical or not, any ideas?