I have tried all the questions and answers on stackoverflow and read and write and rewrite my template, but I keep getting the error "Reverse for 'product_list_by_category' with arguments '('books',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['shop/(?P[-\W]+)/$']"
The full trace is here:
NoReverseMatch at /shop/ Reverse for 'product_list_by_category' with arguments '('books',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['shop/(?P[-\W]+)/$'] Request Method: GET Request URL: http://localhost:3025/shop/ Django Version: 1.9.7 Exception Type: NoReverseMatch Exception Value:
Reverse for 'product_list_by_category' with arguments '('books',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['shop/(?P<category_slug>[-\\W]+)/$']
Exception Location: /home/namalliv/public_html/lib/python/Django-1.9.7-py3.4.egg/django/core/urlresolvers.py in _reverse_with_prefix, line 508
Python Executable: /usr/local/bin/python3.4
Python Version: 3.4.4
Python Path:
['/home/namalliv/public_html/villaproject',
'/usr/local/lib/python3.4/site-packages/python_magic-0.4.11-py3.4.egg',
'/home/namalliv/public_html/lib/python',
'/home/namalliv/public_html/lib/python/Django-1.9.7-py3.4.egg',
'/usr/local/lib/python34.zip',
'/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-linux',
'/usr/local/lib/python3.4/lib-dynload',
'/usr/local/lib/python3.4/site-packages']
Server time: Mon, 5 Dec 2016 01:22:25 +0000
Error during template rendering
In template /home/namalliv/public_html/villaproject/templates/shop/list.html, error at line 22
Reverse for 'product_list_by_category' with arguments '('books',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['shop/(?P<category_slug>[-\\W]+)/$']
12
13 {% block content %}
14 <div id="sidebar">
15 <h3>Catergories</h3>
16 <ul>
17 <li {% if not category %} class="selected" {% endif %}>
18 <a href="{% url 'shop:product_list' %}">All</a>
19 </li>
20 {% for c in categories %}
21 <li {% if category.slug == c.slug %} class="selected" {% endif %}>
22 <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
23 </li>
24 {% endfor %}
25 </ul>
26 </div>
27 <dir id="main" class="product_list">
28 <h1>{% if category %}
29 {{ category.name }} {% else %}Products
30 {% endif %}
31 </h1>
32 {% for product in products %}
Here is my model.py
class Category(models.Model):
name = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
db_index=True, unique=True)
class Meta:
ordering=('name',)
verbose_name = 'caterory'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', kwargs={'slug': self.slug})
view:
def product_list(request, category_slug=None):
category = None;
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'shop/list.html', {'category': category, 'categories': categories, 'products': products})
Here is my urls.py
url(r'^(?P<category_slug>[-\W]+)/$', views.product_list, name='product_list_by_category'),
my main urls.py is
url(r'^shop/', include('shop.urls', namespace='shop')),
And this is my template:
{% block content %}
<div id="sidebar">
<h3>Catergories</h3>
<ul>
<li {% if not category %} class="selected" {% endif %}>
<a href="{% url 'shop:product_list' %}">All</a>
</li>
{% for c in categories %}
<li {% if category.slug == c.slug %} class="selected" {% endif %}>
<a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}
</ul>
</div>
{% endblock content %}
urls.py. - Selcuk