NoReverseMatch at / Reverse for 'product' 没有未找到的参数。尝试了 1 种模式:['product/(?P<slug>[-a-zA-Z0-9_]+)/$']
NoReverseMatch at / Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[-a-zA-Z0-9_]+)/$']
您好,我正在学习 Django,对此我还是个新手。
我想要这样的网址:www.mysite.com/product/product-1 但我不知道它不起作用
models.py
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
category = models.CharField(choices=CATEGORY_CHOICES, max_length=6)
slug = models.SlugField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("orders:product", kwargs={
'slug': self.slug
})
views.py
class ItemDetailView(DetailView):
model = Item
template_name = "product.html"
urls.py
from django.urls import path
from .views import HomeView, ItemDetailView, checkout
urlpatterns = [
path("", HomeView.as_view(), name="home"),
path("product/<slug>/", ItemDetailView.as_view(), name="product"),
path("checkout/", checkout, name="checkout")
]
这就是您如何在 url.
中获得鼻涕虫
path("product/<slug:slug>/", ItemDetailView.as_view(), name="product"),
您好,我正在学习 Django,对此我还是个新手。 我想要这样的网址:www.mysite.com/product/product-1 但我不知道它不起作用
models.py
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
category = models.CharField(choices=CATEGORY_CHOICES, max_length=6)
slug = models.SlugField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("orders:product", kwargs={
'slug': self.slug
})
views.py
class ItemDetailView(DetailView):
model = Item
template_name = "product.html"
urls.py
from django.urls import path
from .views import HomeView, ItemDetailView, checkout
urlpatterns = [
path("", HomeView.as_view(), name="home"),
path("product/<slug>/", ItemDetailView.as_view(), name="product"),
path("checkout/", checkout, name="checkout")
]
这就是您如何在 url.
中获得鼻涕虫path("product/<slug:slug>/", ItemDetailView.as_view(), name="product"),