Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Book object is not iterable: Trying to display similar objects of an instance based on a field

I am working on a library project that displays books and each both is in a category(history, math, adventure, etc). On the detailed view of a book, I want a link (with "See similar books in this category") that will redirect the user to other books of the same category of the current book. So if the detail page of a book has a category of "history", the link will direct to other books in the "history" category. The redirection is to another template (Addon: In helping me out, I would love to display the "similar books" in the same page, not a different page)

error details

TypeError at /search/similar/bootsrtap/
'Book' object is not iterable

models.py

class Category(models.Model):
name = models.CharField(max_length=255)

def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse('index')
    
choices = Category.objects.all().values_list('name', 'name')
choice_list = []
for item in choices:
    choice_list.append(item)

class Book(models.Model):
title = models.CharField(max_length=255)
author = models.CharField(max_length=255)
category = models.CharField(max_length=255, choices=choice_list)
slug = models.SlugField(max_length=100, unique=True)

def __str__(self):
    return self.author + ": " + self.title

def get_absolute_url(self):
    return reverse('detail', kwargs={'slug': self.slug})

def save(self, *args, **kwargs):
    if not self.slug:
        self.slug = slugify(self.title)
    return super().save(*args, **kwargs)

def similar_book(self):
    return Book.objects.filter(category=self.category)

urls.py

urlpatterns = [
path('detail/<slug:slug>/', BookDetail.as_view(), name='detail'),
path('search/similar/<slug:slug>/', views.search_similar_results, name='search_similar_results'),

views.py

def search_similar_results(request, slug):
    books = get_object_or_404(Book, slug=slug)
    books.similar_book()
    return render(request, 'book/similar_results.html', {'books': books})

class BookDetail(generic.DetailView):
    model = Book
    context_object_name = 'book'
    template_name = 'book/detail.html'

    

clickable link to lead to the template

<a href="{% url 'search_similar_results' slug=book.slug %}">Click to see Similar books in this category</a></h3>
            <div class="row">

template

<div class="row">
        {% if books %}
            {% for book in books %}

            <h2>Similar Books in {{ book.category } Category}</h2>
            <figure class="col-lg-3 col-md-4 col-sm-6 col-12 tm-gallery-item animated bounce infinite">
                <a href="{{ book.get_absolute_url }}">
                <div class="tm-gallery-item-overlay">
                    {% if book.cover %}
                    <img src="{{ book.cover.url }}" alt="Image" class="img-fluid tm-img-center">
                    {% endif %}
                    <p class="small font-italic">Author: {{ book.author }}</p>
                    <p class="small font-italic text-left">Title: {{ book.title }}</p>
                    <p class="small font-italic">Category: {{ book.category }}</p>
                </div>
                </a>
            </figure>
            {% endfor %}
        {% else %}
        <p>There are No Available Books</p>
        {% endif %}

  </div>

details.html

<div class="tm-main-content no-pad-b">
        
    <!-- Book Detail Page Display -->
    <section class="row tm-item-preview">
        {% if book.cover %}
        <div class="col-md-6 col-sm-12 mb-md-0 mb-5">
            <img src="{{ book.cover.url }}" alt="Image" class="img-fluid tm-img-center-sm card-img-top">
            <h2>Title {{ book.title }}</h2>
        </div>
        {% else %}
        <p>No book cover</p>
        {% endif %}
    </section>

    <!-- Similar Books Display by Category -->
    <div class="tm-gallery no-pad-b">
        <h3 style="color: white; text-align: center;"><a href="{% url 'search_similar_results' slug=book.slug %}">Similar books you might be interested in</a></h3>
        <div class="row">
            {% for item in similar_book%}
            <figure class="col-lg-3 col-md-4 col-sm-6 col-12 tm-gallery-item">
                <a href="{{ book.get_absolute_url }}">
                    <div class="tm-gallery-item-overlay">
                        <img src="{{ book.cover.url }}" alt="Image" class="img-fluid tm-img-center">
                    </div>
                    <p class="tm-figcaption">Title: {{ book.title}}</p>
                    <p class="tm-figcaption">Category: {{ book.category }}</p>
                </a>
            </figure>
            {% endfor %}
        </div>   
    </div>                               
</div>  

like image 543
sam hassan Avatar asked Dec 21 '25 20:12

sam hassan


1 Answers

You need to pass the result of the similar books to the template, so:

def search_similar_results(request, slug):
    book = get_object_or_404(Book, slug=slug)
    books = book.similar_book()
    return render(request, 'book/similar_results.html', {'books': books})

In the DetailView, you can pass the `similar books with:

class BookDetail(generic.DetailView):
    model = Book
    context_object_name = 'book'
    template_name = 'book/detail.html'

    def similar_books(self):
        return self.object.similar_book()

Then in the template we work with:

{% for item in view.similar_books %}
    <!-- … -->
{% endfor %}
like image 173
Willem Van Onsem Avatar answered Dec 23 '25 09:12

Willem Van Onsem