Django 3.0: Getting error for get_absolute_url

Viewed 143

I am getting this error:

TypeError at /product/177042279214449276022367789942330057699/ product() got an unexpected keyword argument 'id'

I am trying to generate detail page of product (book is product).

urls.py

app_name = 'bookrepo'

urlpatterns = [
    path('',views.home,name='home'),
    path('product/',views.product,name='product'),
    path('product/<id>/', views.product, name='product_detail'),
    ]

template where I am using get_absoulte_url

 <a href="{{ item.get_absolute_url }}" class="btn btn-sm my-btn detail-btn">
     <span><i class="fa fa-info-circle"></i></span> View Details
 </a>

views.py

def product(request):
    return render(request, 'bookrepo/product.html')

models.py

class Book(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField('Title', max_length=255)
    authors = models.ManyToManyField(Author, related_name='books_written')
    publisher = models.ForeignKey(Publisher, on_delete=models.DO_NOTHING, related_name='books_published')
    price = models.DecimalField('Price', decimal_places=2, max_digits=10)
    description = models.TextField('Description')
    upload_timestamp = models.DateTimeField('Uploading DateTime', auto_now_add=True)
    categories = models.ManyToManyField(Category, related_name='book_category')

    def get_absolute_url(self):
        return "/product/%i/" % self.id

I might be completely wrong with respect to my view and urls. I want to display book details after button in template gets clicked.

1 Answers

Change views.py

def product(request, id=None):
    return render(request, 'bookrepo/product.html')
Related