pythondjangobookmarksfavorites

how show favorite list in django


I have a list of favorites and I want to show them when I click on the interest button after I click on the list and my heart will be bold. The second part, ie filling the heart, is done correctly, but when I want to show the list, it does not show anything and gives the following error.

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

Solution

  • you use {% url 'show_Book' Book.BookID %} but you don't have any definition in the urls.py with name show_Book try changing

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

    to

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