cipher
cipher

Reputation: 393

NoReverseMatch at / Reverse for 'single_product' with no arguments not found. 1 pattern(s) tried: ['products/(?P<slug>)/$']

I got this error using django 2.2 here are my codes

urls.py

app_name = 'products'

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 in product model

def get_absolute_url(self,):
        return HttpResponseRedirect(reverse('single_product',args=[self.slug]))

tempplate

<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>

Upvotes: 3

Views: 6627

Answers (1)

Exprator
Exprator

Reputation: 27523

you are not passing the slug in the url but have given a parameter to your pattern.

so change the html

<a href ="{% url 'products:single_product' product.slug %}" class = "btn btn-primary" role = "button">
      View Product
   </a>

Upvotes: 7

Related Questions