2

I am really having hard time to understand ways to dispatch arguments and keword arguments to Django urls. Following is the case study:

I've a view using generic base view:

class CartView(View):
   def get(self, request, *args, **kwargs):
       item = request.GET.get('item')
       qty = request.GET.get('qty')
       print item, qty
       return HttpResponseRedirect('/')

With above view I was able to work with aurguments in url like "localhost:8000/cart/?item=4&qty=200" and it prints the item with quantity in terminal.

As soon I've made changes in code like:

from carts.models import Cart, CartItem
from products.models import Variation


class CartView(View):
    def get(self, request, *args, **kwargs):
        item_id = request.GET.get('item')
        if item_id:
            item_instance = get_object_or_404(Variation, id=item_id)
            qty = request.GET.get('qty')
            cart = Cart.objects.all()[0]
            cart_item = CartItem.objects.get_or_create(cart=cart, item=item_instance)[0]
            cart_item.quantity = qty
            cart_item.save()
            print cart_item
        return HttpResponseRedirect('/')

With same way passing arguments like "localhost:8000/cart/?item=4&qty=200" it shows me the error:

404 Page Not Found No Variation matches the given query.

urls.py

urlpatterns = [
    url(r'^home/$', 'newsletter.views.home', name='home'),
    url(r'^contact/$', 'newsletter.views.contact', name='contact'),
    url(r'^about/$', 'project.views.about', name='about'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('registration.backends.default.urls')),
    url(r'^cart/$', CartView.as_view(), name='cart'),
    url(r'^', include('products.urls')),
    url(r'^categories/', include('products.urls_categories')),
tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
Ali khan
  • 515
  • 1
  • 5
  • 18
  • Possible duplicate of [Is it possible to pass query parameters via Django's {% url %} template tag?](http://stackoverflow.com/questions/4591525/is-it-possible-to-pass-query-parameters-via-djangos-url-template-tag) – Sayse Jun 29 '16 at 07:26
  • 2
    Because you have no Variation object with ID 4 – Antoine Pinsard Jun 29 '16 at 07:31
  • I passed the argument with even products having variation objects. Same issue. Secondly I've create a post_save signal to convert all products with default variation to handle it better in carts. Now I simply implement cart on variations. – Ali khan Jun 29 '16 at 07:34
  • That was my mistake that I've deleted some test products and IDs got changed upto 10. Thing is that DB if IDs saved 1 to 10 and we delete past products then newly saved products will have IDs above 10. I went to manage.py shell and imported Variation there. Then saved Variation.objects.all().first() in a variable. When I call variable.id it showed me 14 for the first product. So when I passed argument with item=14 it accepted it. @Antoine Pinsard you were right. Post your answer and I will mark it. – Ali khan Jun 29 '16 at 08:10

1 Answers1

2

404 Page Not Found

No Variation matches the given query.

This message comes from your line:

item_instance = get_object_or_404(Variation, id=item_id)

And means that you have no Variation object matching the given id.

Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87