gpt4 book ai didi

python - 图书对象不可迭代 : Trying to display similar objects of an instance based on a field

转载 作者:行者123 更新时间:2023-12-04 14:57:04 26 4
gpt4 key购买 nike

我正在从事一个展示书籍的图书馆项目,每本书都属于一个类别(历史、数学、冒险等)。在一本书的详细 View 中,我想要一个链接(带有“查看该类别中的类似书籍”),它将用户重定向到与当前书籍相同类别的其他书籍。因此,如果一本书的详细信息页面有“历史”类别,则链接将指向“历史”类别中的其他书籍。重定向到另一个模板(插件:在帮助我的过程中,我很乐意在同一页面而不是不同的页面中显示“类似的书”)

错误详情

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

模型.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'



指向模板的可点击链接

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

模板

<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>

最佳答案

您需要将类似书籍的结果传递给模板,因此:

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

DetailView 中,您可以传递类似的书籍:

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

def similar_books(self):
return self<b>.object.similar_book()</b>

然后在我们使用的模板中:

{% for item in <b>view.similar_books</b> %}
<!-- … -->
{% endfor %}

关于python - 图书对象不可迭代 : Trying to display similar objects of an instance based on a field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67752746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com