I am 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 is my templates/menu.htmlfile -
...
...
{% 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 %}
...
...
orders/urls.py -
....
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.pyis
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, orders/views.pyit looks like this:
def add_to_cart(request, product_id):
product = Inventory.objects.get(id=product_id)
....
In this case, the home page that causes the page menu.htmlproduces an error -
Reverse for 'addCart' with arguments '(2,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
It says that the error while rendering the template, especially in the line -
<a href="{% url 'addCart' id %}">+</a>
, .
orders:addCart . BUt . - , , orders, ? .