Home > Enterprise >  WSGIRequest' object has no attribute 'session_key
WSGIRequest' object has no attribute 'session_key

Time:11-16

enter image description here

I am getting this error when i try to access the session I am not able to understand why it is not understanding what is session it is in installed apps it knows what is session

def _cart_id(request):
    cart = request.session_key
    if not cart:
        cart = request.session.create()
    return cart

def add_cart(request,id):
    prod = Product.objects.get(id = id)
    try:
        cart = Cart.objects.get(cart_id = _cart_id(request))

    except Cart.DoesNotExist:
        cart = Cart.objects.create(
            cart_id = _cart_id(request)
        )
    cart.save()

    try:
        cart_item = CartItem.object.get(product = prod,cart = cart)
        cart_item.quantity  = cart_item.quantity
    except CartItem.DoesNotExist:
        cart_item = CartItem.objects.create(
            product = prod,
            quantity = 1,
            cart = cart, 
        )
        cart_item.save()

    return redirect('/shop/') 

CodePudding user response:

The request object has no session_key but session. And session_key is inside session. Then :

def _cart_id(request):
    # Not request.session_key but request.session.session_key
    cart = request.session.session_key
    if not cart:
        cart = request.session.create()
    return cart
  • Related