1

I`m using Django 1.10 and I getting this error:

NoReverseMatch at / Reverse for 'views.product_detail' with arguments '()' and keyword arguments '{'pk': 1}' not found. 0 pattern(s) tried: []

This is my index.html template:

{% for pr in product %}
                <li>
                    {{ pr.pk }}
                    <a href="{% url 'views.product_detail' pk=pr.pk %}">
                        {{ pr.name }}
                    </a>
                    || {{ pr.description }} || <img src="{{ pr.image.url }}">
                </li>
{% endfor %}

My Main project urls.py is:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', include('products.urls') ),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

My App urls.py is:

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

urlpatterns = [
    url(r'^$', views.hello_world, name='hello'),
    url(r'^product/(?P<pk>[0-9]+)/$', views.product_detail,name='product_detail'),
]

My views.py is:

from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.shortcuts import render, get_object_or_404
from .models import Product

# Create your views here.
def hello_world(request):
    product = Product.objects.order_by('id')
    template = loader.get_template('index.html')
    context = {
        'product': product
    }
    return HttpResponse(template.render(context, request))

def product_detail(request, pk):
    product = get_object_or_404(Product, pk=pk)
    template = loader.get_template('product_detail.html')
    context = {
        'product': product
    }
    return HttpResponse(template.render(context, request))

the error message is: Error message

Thanks for your help !!!

  • 2
    You shouldn't be using URL tag like that. Instead, use the permalink decorator. https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#get-absolute-url – Andrey Shipilov Mar 27 '17 at 03:12

2 Answers2

0

The problem is how you've included the product URLs. You've terminated that including pattern with $, so nothing afterwards will ever match. When including, never use a $.

url(r'', include('products.urls') ),
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
-1

NoReverseMatch means that Django hasn't matched your requested URL with any URL you have in your urls.py.

It looks like r'^$' wouldn't find anything because the pattern has no room to include any text. Change r'^$' to r'' or r'^products/' in your project urls. That way, your urls would look like this:

'/products/product/1/' # For product_detail
'/products/' # For views.hello_world
Kris Molinari
  • 503
  • 6
  • 17
  • Hi Kris, I change r'^$' to r'^/$' and now the message is **Page not found (404) Request Method: GET Request URL: http://localhost:8000/ Using the URLconf defined in Shoppy.urls, Django tried these URL patterns, in this order: ^admin/ ^/$ [name='hello'] products/(?P[0-9]+)/$ [name='product_detail'] ^media\/(?P.*)$ The current URL, , didn't match any of these.** Thanks for your help – Claudio Rojas Mar 28 '17 at 09:47