Erik Barajas
Erik Barajas

Reputation: 193

NoReverseMatch at / Python Django

I'm taking a Django course, and I'm having the next error:

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

I'm trying to send an argument to a view from a file that is called index.html

My index.html looks like this:

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

I already declared the url that is associated:

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

And my views.py looks like this:

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

Does someone know why is this error happening?

Thanks.

Upvotes: 3

Views: 575

Answers (1)

knbk
knbk

Reputation: 53669

From "Features to be removed in 1.10":

  • The ability to reverse() URLs using a dotted Python path is removed.

The {% url %} tag uses reverse(), so the same applies. As elethan mentioned in the comments, you need to use the name parameter provided in your URLconf instead, in this case views.product_detail:

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

Upvotes: 6

Related Questions