【发布时间】:2020-08-26 08:27:51
【问题描述】:
我收到此错误:
/product/177042279214449276022367789942330057699/处的类型错误 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
我的观点和网址可能完全错误。我想在模板中的按钮被点击后显示书籍详细信息。
【问题讨论】:
-
请改用
/product/%s/" % self.id,%i会将您的 uuid 转换为一个很大的数字 -
@minglyu 同样的错误:
product() got an unexpected keyword argument 'id'。我认为问题出在urls.py或视图中
标签: django django-views django-templates django-urls