Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<slug>[-a-zA-Z0-9_]+)\\Z']

in models

   def get_absolute_url(self):
      return reverse("book-detail", args={self.slug})

in views

def book_detail(request,slug):
    # try:
    #  book = Book.objects.get(pk=id)
    
    # except: 
    #  raise Http404()
    book = get_object_or_404(Book, slug=slug)
    return render(request ,"book_outlet/book_detail.html",{
        "title" : book.title,
        "author" : book.author,
        "rating" : book.rating,
        "is_best_seller" :book.is_best_selling

        
    } )
ursl.py
urlpatterns = [
    path("", views.index),
    path("<slug:slug>", views.book_detail, name="book-detail")
]

Welcome! When posting code here, enclose the code (or templates, error messages, etc) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.
(I’ve taken the liberty of doing this for you for this post.)

The line being highlighted in the error (the <a> tag) is trying to call book.get_absolute_url to get a url.

That function is trying to reverse the url named book-detail, which requires a parameter for slug.

In your function you have:

but the docs show that you should be passing the parameter as a list and not a set. This should be args=[self.slug].

Note, if this does not resolve the issue, the other potential problem is that you could have a book that does not have a slug for a particular instance. Check your database to ensure that every book has a value for slug. (You don’t show your model here, so we can’t check that for other possible issues.)

1 Like

from django.db import models
from django.urls import reverse
from django.core.validators import MinValueValidator , MaxValueValidator
from django.utils.text import slugify

Create your models here.

class Book(models.Model) :
title = models.CharField(max_length=50)
rating = models.IntegerField(validators= [MinValueValidator(1),MaxValueValidator(5)])
author = models.CharField(null= True,max_length=100)
is_best_selling = models.BooleanField(default=False)
slug = models.SlugField(default=“”, null=False , db_index=True)

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


def get_absolute_url(self):
  return reverse("book-detail", args=[self.slug])



def __str__(self) :
    return f"{self.title} ,({self.rating})"

yes still the issue persist ,i m sending the model here .Please help

You need to verify that every instance of Book has a value for slug. You need to inspect your database for that.

Also note that the problem is not in book_detail. You haven’t gotten that far. The error message is telling you that the problem exists in the index view. (See the line Raised during.)

1 Like

Thanks alot Sir , u 've save my day
issue is absent slug value in db and the args=[self.slug].