Implementing a django cart using Django cart module Error: unhashable type dict

31 views
Skip to first unread message

carl collins

unread,
Sep 4, 2018, 9:14:40 AM9/4/18
to Django users
from django.template import loader
from django.http import HttpResponse
from django.template.loader import render_to_string
import django.template.loader
# Create your views here.

# Create your views here.

from cart.cart import Cart
from .models import Product
'''from cart.models import Category, Product'''


#gallery    default view
def index(request):
    #prepare the models
    #        empty
    #prepare the template
    template = loader.get_template('templates/cart.html', dict(cart=Cart(request))  )
    #prepare the context
    context = {'request':request}
    return HttpResponse(template.render(context, request))

def add_to_cart(request, product_id, quantity):
    product = Product.objects.get(id=product_id)
    cart = Cart(request)
    cart.add(product, product.unit_price, quantity)

def remove_from_cart(request, product_id):
    product = Product.objects.get(id=product_id)
    cart = Cart(request)
    cart.remove(product)


    def get_cart(request):
    

    template = loader.get_template('templates/cart.html', dict(cart = Cart(request))) 

    #prepare the context
    context = {'request':request}
    return HttpResponse(template.render(context, request))
    return HttpResponse(template.render(context, request))

Capture.PNG

pgopa...@gmail.com

unread,
Sep 4, 2018, 11:11:56 AM9/4/18
to Django users
Dear Carl,

  I'm sorry, I've no idea what the Cart is. I'm assuming that it is a class which can be instantiated with some dictionary data.
  So you try passing request.data instead of just request to the Cart.

Try to check by printing request.data, it'll be a dict.
FYI you can check the below link, may be you can find the Root cause for your error.


Thanks,
Gopal

carl collins

unread,
Sep 5, 2018, 12:39:06 AM9/5/18
to django...@googlegroups.com
Thanks Gopal

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc1db1cf-b36f-434c-95b1-df82f631dd38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages