1
votes

Cart App

views.py

from django.shortcuts import render, HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.

from products.models import Product
from .models import Cart 

def view(request):
   cart = Cart.objects.all()[0]
   context = {"cart": cart}
   template = "cart/view.html"
   return render(request, template, context)

def update_cart(request, id):
   cart = Cart.objects.all()[0]
   try:
       product = Product.objects.get(id=id)
   except Product.DoesNotExist:
       pass
   except:
       pass
   if not product in cart.products.all():
       cart.products.add(product)
   else:
       cart.products.remove(product)

   return HttpResponseRedirect(reverse("cart")) 

urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
   # Examples:
    url(r'^$', 'products.views.home', name='home'),
    url(r'^products/$', 'products.views.home', name='products'),
    url(r'^cart/products/$', 'carts.views.update_cart', name='update_cart'),
    url(r'^cart/$', 'carts.views.view', name='cart'),
    url(r'^admin/', include(admin.site.urls)),
) 

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

base.html

   <p><a href='{% url "update_cart" product.id %}' class="btn btn-primary" role="button">Add to cart</a> </p>

According to me there is some fault in the urls.py

Error

NoReverseMatch at /products/ Reverse for 'update_cart' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['carts/products/$'] Request Method: GET Request URL: http://127.0.0.1:8000/products/ Django Version: 1.6.5 Exception Type: NoReverseMatch Exception Value: Reverse for 'update_cart' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$carts/products/$'] Exception Location: /Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 452 Python Executable: /Users/apulgupta/Desktop/table1.2/bin/python Python Version: 2.7.10 Python Path: ['/Users/apulgupta/Desktop/table1.2/table1_2', '/Users/apulgupta/Desktop/table1.2/lib/python27.zip', '/Users/apulgupta/Desktop/table1.2/lib/python2.7', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-darwin', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-mac', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-tk', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-old', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages'] Server time: Tue, 28 Feb 2017 00:23:35 +0530 Error during template rendering

In template /Users/apulgupta/Desktop/table1.2/table1_2/templates/base.html, error at line 83 Reverse for 'update_cart' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$carts/products/$']

2

2 Answers

0
votes

You should add regex to catch the ID.

so the URL should be

url(r'^cart/products/(?P<product_id>\d+)$', 'carts.views.update_cart', name='update_cart'),

In urls.py and the use the id in the views.py

2
votes

The traceback points to the problem url pattern: '^$carts/products/$'. You have a stray $ sign at the start, which means the rest of the pattern will not be matched.