我使用 django 2.2 时遇到此错误,这是我的代码
urls.py
app_name = '产品'
urlpatterns = [
url(r'^$', product_list, name='product-list'),
url(r'^(?P<slug>.*)/$',single, name="single_product"),
url(r'^category/(?P<slug>.*)/$',category_single,name="category")
]
产品模型中的views.py
def get_absolute_url(self,):
return HttpResponseRedirect(reverse('single_product',args=[self.slug]))
模板
<h3>{{ product }}</h3>
<p>{{ product.description }}</p>
<p>{{ product.get_price }}</p>
<p>
<a href ="{% url 'products:single_product' %}" class = "btn btn-primary" role = "button">
View Product
</a>
最佳答案
您没有在 url 中传递 slug,而是为您的模式提供了一个参数。
所以改变html
<a href ="{% url 'products:single_product' product.slug %}" class = "btn btn-primary" role = "button">
View Product
</a>
关于python - NoReverseMatch at/Reverse 为 'single_product',未找到任何参数。尝试了 1 个模式 : ['products/(?P<slug>)/$' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56662329/