如何在 Django 中显示收藏夹列表

how show favorite list in django

我有一个收藏夹列表,我想在点击列表后点击兴趣按钮时显示它们,我的心会很大胆。第二部分,即填充心脏,正确完成,但是当我想显示列表时,它没有显示任何内容并给出以下错误。

Reverse for 'show_Book' not found. 'show_Book' is not a valid view function or pattern name.

model.py

class Book (models.Model):
    BookID= models.AutoField(primary_key=True)
    Titel=models.CharField(max_length=150 )
    Author=models.ForeignKey('Author',on_delete=models.CASCADE)
    Publisher=models.ForeignKey('Publisher',on_delete=models.CASCADE)      translator=models.ForeignKey('Translator',null='true',blank='true',on_delete=models.SET_NULL)
    favorit=models.ManyToManyField('orderapp.Customer',related_name='favorit', blank=True)
    

view.py

def show_Book(request,BookID):
    showBook=get_object_or_404(Book,BookID=BookID)
    is_favorite=False
        if showBook.favorit.filter(id=request.user.id).exists():
        is_favorite=True
    return render (request , 'showBook.html', {'is_favorite':is_favorite,})

def favoritbook (request, BookID):
    showBook=get_object_or_404(Book,BookID=BookID)
    if showBook.favorit.filter(id=request.user.id).exists():
        showBook.favorit.remove(request.user)
    else:
        showBook.favorit.add(request.user)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))


def favoritlist(request):
    user=request.user
    favoritbooks=user.favorit.all()
    context={'favoritbooks':favoritbooks}
    return render (request,'favotritlist.html',context)

url.py

path('showbookstor/<int:id>', views.show_BookStor, name='show_BookStor'),
path('books/favorit/<int:BookID>/', views.favoritbook, name='favoritbook'),
path('books/favorit/', views.favoritlist, name='favoritlist'),

showbook.html

{% if is_favorite %}
    <li id="sell"><a href="{% url 'favoritbook' showBook.BookID %}">add to favorit <i class="fas fa-heart"></i></a> </li>
{% else %}
    <li id="sell"><a href="{% url 'favoritbook' showBook.BookID %}"> delete favorit <i class="far fa-heart"></i></a>    </li>
{% endif %}

favoritlist.html

{% for Book in favoritbooks %}
    <section id="card" class="col-md-6 col-lg-3 col-sm-6 col-xs-12">
    <a href="{% url 'show_Book' Book.BookID %}"><img src= {{Book.Image.url}}></a>
    <h1 id="bookname">{{Book.Titel}}</h1>
                        
    <p>{{Book.Author}}</p>
    </section>
{% endfor %}

您使用 {% url 'show_Book' Book.BookID %} 但您在 urls.py 中没有任何名称 show_Book 的定义尝试更改

path('showbookstor/<int:id>', views.show_BookStor, name='show_BookStor')

path('showbook/<int:id>', views.show_Book, name='show_Book')