Python Book对象不可编辑:尝试基于字段显示实例的类似对象
我正在做一个图书馆项目,展示书籍,每一本都属于一个类别(历史、数学、冒险等等)。在一本书的详细视图中,我需要一个链接(带有“查看此类别中的类似书籍”),该链接将用户重定向到与当前书籍相同类别的其他书籍。因此,如果一本书的详细页面有一个“历史”类别,则链接将指向“历史”类别中的其他书籍。重定向到另一个模板(插件:在帮助我的过程中,我希望在同一页面中显示“类似的书籍”,而不是在不同的页面) 错误详细信息Python Book对象不可编辑:尝试基于字段显示实例的类似对象,python,django,django-models,django-queryset,django-filter,Python,Django,Django Models,Django Queryset,Django Filter,我正在做一个图书馆项目,展示书籍,每一本都属于一个类别(历史、数学、冒险等等)。在一本书的详细视图中,我需要一个链接(带有“查看此类别中的类似书籍”),该链接将用户重定向到与当前书籍相同类别的其他书籍。因此,如果一本书的详细页面有一个“历史”类别,则链接将指向“历史”类别中的其他书籍。重定向到另一个模板(插件:在帮助我的过程中,我希望在同一页面中显示“类似的书籍”,而不是在不同的页面) 错误详细信息 TypeError at /search/similar/bootsrtap/ 'Book' o
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)
url.py
urlpatterns = [
path('detail/<slug:slug>/', BookDetail.as_view(), name='detail'),
path('search/similar/<slug:slug>/', views.search_similar_results, name='search_similar_results'),
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>
模板
<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>
{%if书籍%}
{书籍中书籍的百分比%}
{{book.category}category}中的类似书籍
{%endfor%}
{%else%}
没有可用的书
{%endif%}
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>
{%if book.cover%}
书名{book.Title}
{%else%}
没有书皮
{%endif%}
{类似书籍%中的项目的百分比}
{%endfor%}
您需要将类似书籍的结果传递给模板,以便:
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})
然后在我们使用的模板中:
{% for item in view.similar_books %}
<!-- … -->
{% endfor %}
{%用于视图中的项目。类似\u books%}
{%endfor%}
你真棒@Willem Van Onsem。你刚刚解除了我的压力。对于我的插件,我如何在同一个“详细信息页面”中显示“类似结果”。我不想重定向到另一个页面。所以用户应该在同一个页面上看到类似的书籍page@samhassan:然后,您应该将逻辑添加到显示详细信息的视图中,并将其通过书籍详细信息视图的模板传递。然后,我将如何重新构造url以指向详细信息页面。它给出了这个错误:找不到关键字参数为“{slug':”}”的“搜索类似结果”的/search/similor/bootsrtap/Reverse处的NoReverseMatch。尝试了1种模式:[“搜索/相似/(?P[-a-zA-Z0-9_]+)/$”@samhassan:这是因为slug是空的,或者您使用了一个不存在的变量。我非常感谢您的时间。我对Django比较陌生。你能帮我重构一下代码吗。这是我的第一个问题