Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoReverseMatch at / Python Django

Tags:

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.

like image 963
Erik Barajas Avatar asked Feb 26 '26 17:02

Erik Barajas


1 Answers

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 %}
like image 107
knbk Avatar answered Mar 01 '26 07:03

knbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!