Django 3.0:获取get_absolute_url的错误
我得到这个错误: 在/product/1770422792449276022367789942330057699处的类型错误/ product()获得意外的关键字参数“id” 我试图生成产品的详细页面(书就是产品) url.pyDjango 3.0:获取get_absolute_url的错误,django,django-views,django-templates,django-urls,Django,Django Views,Django Templates,Django Urls,我得到这个错误: 在/product/1770422792449276022367789942330057699处的类型错误/ product()获得意外的关键字参数“id” 我试图生成产品的详细页面(书就是产品) url.py app_name = 'bookrepo' urlpatterns = [ path('',views.home,name='home'), path('product/',views.product,name='product'), path
app_name = 'bookrepo'
urlpatterns = [
path('',views.home,name='home'),
path('product/',views.product,name='product'),
path('product/<id>/', views.product, name='product_detail'),
]
def product(request):
return render(request, 'bookrepo/product.html')
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
def product(request, id=None):
return render(request, 'bookrepo/product.html')
型号.py
app_name = 'bookrepo'
urlpatterns = [
path('',views.home,name='home'),
path('product/',views.product,name='product'),
path('product/<id>/', views.product, name='product_detail'),
]
def product(request):
return render(request, 'bookrepo/product.html')
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
def product(request, id=None):
return render(request, 'bookrepo/product.html')
关于我的视图和URL,我可能完全错了。我想在单击模板中的按钮后显示书籍详细信息。更改视图。py
app_name = 'bookrepo'
urlpatterns = [
path('',views.home,name='home'),
path('product/',views.product,name='product'),
path('product/<id>/', views.product, name='product_detail'),
]
def product(request):
return render(request, 'bookrepo/product.html')
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
def product(request, id=None):
return render(request, 'bookrepo/product.html')
使用
/product/%s/%self.id
代替,%i
将您的uuid转换为一个大的number@minglyu同样的错误:product()得到了一个意外的关键字参数“id”
。我认为问题出在url.py
或视图中