deathstroke
deathstroke

Reputation: 526

Django- NoReverseMatch. Reverse for '' with arguments '(2,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

I'm trying to pass a value from my template file to a function in the views.py file in Django.

My project structure is as follows-

myproject/
    manage.py
    myproject/
        __init__.py
        urls.py
        wsgi.py
        views.py
        settings.py
    orders/
        __init__.py
        models.py
        views.py
        urls.py
        tests.py
    restaurant/
        __init__.py
        models.py
        views.py
        urls.py
        tests.py

     requirements.txt

Here's my templates/menu.html file -

...
...    
{% for id,image,menu in imageList %}
    <div style = "display:inline-block">
        <img src="{{ MEDIA_URL }}{{ image }}">
        <p>{{ menu }}</p>
        <a href="{% url 'addCart' id %}">+</a>
        <a href="">-</a>
    </div>
{% endfor %}
...
...

The orders/urls.py is -

....
from orders.views import add_to_cart

urlpatterns = patterns('',
    url(r'^add/(?P<product_id>\d+)$', add_to_cart, name ='addCart'),
)

And the root urls.py is -

from orders.views import *

urlpatterns = patterns('',
    url(r'^$', menu),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^orders/', include('orders.urls', namespace = "addCart")),
)

And finally, the orders/views.py is as follows-

def add_to_cart(request, product_id):
    product = Inventory.objects.get(id=product_id)
    ....

On executing this, the homepage, which calls the menu.html page gives the error-

Reverse for 'addCart' with arguments '(2,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

It says that the error is during the template rendering, specifically at the line -

<a href="{% url 'addCart' id %}">+</a>

I've tried a lot of different solutions, but nothing seems to work. I've tried using orders:addCart in the tag, too. BUt it doesn't work. Is it because of the views that I'm importing from the orders app is of the wrong format? Thank you.

Upvotes: 4

Views: 4018

Answers (1)

knbk
knbk

Reputation: 53669

Your url lives in the namespace 'addCart', so you'll have to specify that when reversing the url:

{% url 'addCart:addCart' id %}

Upvotes: 5

Related Questions