Ali khan
Ali khan

Reputation: 585

Django urls not taking arguments

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')),

Upvotes: 2

Views: 178

Answers (1)

Antoine Pinsard
Antoine Pinsard

Reputation: 34942

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.

Upvotes: 2

Related Questions