我已经尝试了stackoverflow上的所有问题和答案,读写并重写了我的模板,但我不断收到错误"反向' product_list_by_category'有参数'(' books',)'和关键字参数' {}'未找到。尝试了1种模式:[' shop /(?P [ - \ W] +)/ $']"
完整的痕迹在这里:
> / shop /的NoReverseMatch 反转&#39; product_list_by_category&#39;有参数&#39;(&#39; books&#39;,)&#39;和关键字参数&#39; {}&#39;未找到。尝试了1种模式:[&#39; shop /(?P [ - \ W] +)/ $&#39;] 请求方法:GET 请求网址:http://localhost:3025/shop/ Django版本:1.9.7 异常类型:NoReverseMatch 例外值: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 %}
这是我的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})
视图:
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})
这是我的urls.py
url(r'^(?P<category_slug>[-\W]+)/$', views.product_list, name='product_list_by_category'),
我的主要urls.py是
url(r'^shop/', include('shop.urls', namespace='shop')),
这是我的模板:
{% 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 %}
答案 0 :(得分:1)
您的网址格式使用\W
(大写W
),它匹配除字母数字字符之外的所有内容。您需要将其更改为小写,以便它与字母数字字符匹配:
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),