Haegyun Jung
Haegyun Jung

Reputation: 755

Django Error for NoReverseMatch

I'm a student which studying python django.

I was encountered below error message.

NoReverseMatch at /
Reverse for 'product' with arguments '(2,)' and keyword arguments '{}' not found.
1 pattern(s) tried: ['$(?P<pk>[0-9]+)$']

I tried to search and search from yesterday, but I'm really don't know the reason.

Could you please give me an any tip or a recommendation?

Django version that i using is 1.7.

==================================================================================

project_root/shops/templates/shops/base.html

 <!-- **  error occurs at this point  ** -->
 <li><a href="{% url 'shops:product' category.id %}" >

project_root/project/urls.py

 from django.conf.urls import include, url
 from django.contrib import admin
 from django.conf import settings

 urlpatterns = [
     url(r'^$', include('shops.urls', namespace="shops")),
     url(r'^admin/', include(admin.site.urls)),
 ]

 if settings.DEBUG:
     urlpatterns += [
         url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
             'document_root': settings.MEDIA_ROOT,
         }),
         url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
             'document_root': settings.STATIC_ROOT,
         }),
 ]  

project_root/shops/urls.py

from django.conf.urls import url
from shops import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)$', views.ProductView.as_view(), name='product'),
]

project_root/shops/views.py

from django.views.generic.base import TemplateView 
from django.views.generic.list import ListView
from django.utils import timezone

from shops.models import Sex, Category, Product


class IndexView(TemplateView):
    template_name = 'shops/index.html'

    def get_context_data(self):
        context = super(IndexView, self).get_context_data()
        context['sex_list'] = Sex.objects.all()
        context['category_list'] = Category.objects.all()
        context['latest_product_list'] = Product.objects.order_by('-pub_date')[:5]

        return context

class ProductView(ListView):
    template_name = 'shops/product_list.html'

    def get_context_data(self):
        context = super(ProductView, self).get_context_data()
        context['product_list'] = Product.objects.all()
        return context

Upvotes: 0

Views: 660

Answers (1)

rfj001
rfj001

Reputation: 8508

You need to change url(r'^$', include('shops.urls', namespace="shops")), in project_root/project/urls.py to just url(r'^', include('shops.urls', namespace="shops")), The $ means match strings with characters exactly the same as preceding the $, so when you have the pk in your project_root/shops/urls.py the pk does not get considered because the $ specified in the original url where you include all your project_root/shops/urls.py cuts the regex off short.

That probably could have been worded better... but hopefully you get the point. Urls that are used to include other url files should almost never contain a $

Upvotes: 2

Related Questions