Django 3.0:get_absolute_url出错

问题描述 投票:1回答:1

我收到此错误:

/ product / 177042279214449276022367789942342330057699 /product()得到了意外的关键字参数'id'

我正在尝试生成产品的详细信息页面(书是产品)。

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'),
    ]

我正在使用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

我的观点和网址可能完全错了。单击模板中的按钮后,我想显示书籍详细信息。

django django-views django-templates django-urls
1个回答
1
投票
views.py

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

© www.soinside.com 2019 - 2024. All rights reserved.