Re: problem with simple shopping cart

2009-02-09 Thread lnysrogh

I'm also getting an Index Error, "list index out of range" for the
"request.session['cart'][1] += 1" line. I can't figure out what's
wrong.

On Feb 9, 3:02 am, lnysrogh <lordnysr...@gmail.com> wrote:
> Thank you so much. That helped a lot. Such a simple mistake.
>
> I've got some other questions now. My "add to cart" code:
>
> def add(request, product_id):
>         added = (Product.objects.get(id=product_id), 1) # '1' is quantity
>         request.session.set_test_cookie()
>         if request.session.test_cookie_worked():
>                 request.session.delete_test_cookie()
>                 if 'cart' in request.session:
>                         if added in request.session['cart']:
>                                 request.session['cart'][1] += 1
>                         else:
>                                 request.session['cart'].append(added)
>                 else:
>                         request.session['cart'] = []
>                         request.session['cart'].append(added)
>                 return HttpResponseRedirect('/cart/')
>         else:
>                 return HttpResponse('You need to enable cookies')
>
> The code worked until I tried to check if the product is already
> stored in the cart and to add 1 to the quantity instead of storing it
> again. I'm getting this error: "can only concatenate tuple (not "int")
> to tuple". What am I doing wrong?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: problem with simple shopping cart

2009-02-08 Thread lnysrogh

Thank you so much. That helped a lot. Such a simple mistake.

I've got some other questions now. My "add to cart" code:

def add(request, product_id):
added = (Product.objects.get(id=product_id), 1) # '1' is quantity
request.session.set_test_cookie()
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
if 'cart' in request.session:
if added in request.session['cart']:
request.session['cart'][1] += 1
else:
request.session['cart'].append(added)
else:
request.session['cart'] = []
request.session['cart'].append(added)
return HttpResponseRedirect('/cart/')
else:
return HttpResponse('You need to enable cookies')

The code worked until I tried to check if the product is already
stored in the cart and to add 1 to the quantity instead of storing it
again. I'm getting this error: "can only concatenate tuple (not "int")
to tuple". What am I doing wrong?

Also, Andrew, I tried storing only the product id in the session
instead of the product object, and calling it in the show cart page.
Removing the "quantity" part of my code to simplify since it's not
working, I tried this:

Using the code above, I stored just the product_id in added, not the
Product.objects.get(id).

def cart(request):
if 'cart' in request.session:
cart = request.session['cart']
for item in cart:
cart = Product.objects.get(id=item)
return render_to_response('cart.html', {'cart': cart})
else:
return HttpResponse('Nenhum produto')

I get this: "int() argument must be a string or a number, not
'tuple'".

It seems there is one concept I'm not getting right, since I'm getting
similar error messages for totally different problems.

Any help?

Thanks.

On Feb 2, 10:14 pm, Andrew Ingram <a...@andrewingram.net> wrote:
> lnysrogh wrote:
> > Hello. I'm new to Django (and web development in general), and this is
> > my first post here. Hope someone can help me.
>
> > I'm trying to build a shopping cart, and my idea is to store the
> > product information (id and quantity) in a session when I add the
> > product to the shopping cart, and retrieve it to display in the "show
> > cart" page. I'm using sessions for this because I don't want the user
> > to have to log in or register just to add some items to the cart and
> > see the total price. When he logs in to actually buy something, I
> > pretend to store it in a cart model, but I didn't get there yet. I'll
> > show you the code I'm testing:
>
> > def add(request, product_id):
> >    request.session.set_test_cookie()
> >    if request.session.test_cookie_worked():
> >            request.session.delete_test_cookie()
> >            added = Product.objects.get(id=product_id)
> >            request.session['product_id'] = added
> >            return HttpResponseRedirect('/cart/')
> >    else:
> >            return HttpResponse('You need to enable cookies')
>
> > def cart(request):
> >    if 'product_id' in request.session:
> >            cart = request.session['product_id']
> >            return render_to_response('cart.html', {'cart': cart})
> >    else:
> >            return HttpResponse('Cart is empty')
>
> > That's what I was able to come up with. The first problem is, it's not
> > working. The second problem is, I can't figure out how to add more
> > than one product to the cart.
>
> > The error I'm getting when I add something to the cart is "Caught an
> > exception while rendering: 'Product' object is not iterable". I'm
> > pretty sure that error has something to do with the cart.html, because
> > if I replace that line with:
>
> > return HttpResponse('You've added the product %s to the cart' % cart)
>
> > it works. The cart.html is just this:
>
> > {% for product in cart %}
> > Your cart: {{ product.name }} - {{product.price}}
> > {% endfor %}
>
> > It should work, right? I just can't figure what's causing that error.
>
> I've noticed a few things that you should consider:
>
> Firstly, you're storing the actual product object in the session which
> is less than ideal, it'd be better to load any cart objects from the db
> at the start of each request. This prevents the problem of a session
> product and the real db product getting out of sync if a change is made.
>
> Second problem is that you're assigning request['product_id'] to an
> actual

problem with simple shopping cart

2009-02-02 Thread lnysrogh

Hello. I'm new to Django (and web development in general), and this is
my first post here. Hope someone can help me.

I'm trying to build a shopping cart, and my idea is to store the
product information (id and quantity) in a session when I add the
product to the shopping cart, and retrieve it to display in the "show
cart" page. I'm using sessions for this because I don't want the user
to have to log in or register just to add some items to the cart and
see the total price. When he logs in to actually buy something, I
pretend to store it in a cart model, but I didn't get there yet. I'll
show you the code I'm testing:

def add(request, product_id):
request.session.set_test_cookie()
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
added = Product.objects.get(id=product_id)
request.session['product_id'] = added
return HttpResponseRedirect('/cart/')
else:
return HttpResponse('You need to enable cookies')

def cart(request):
if 'product_id' in request.session:
cart = request.session['product_id']
return render_to_response('cart.html', {'cart': cart})
else:
return HttpResponse('Cart is empty')

That's what I was able to come up with. The first problem is, it's not
working. The second problem is, I can't figure out how to add more
than one product to the cart.

The error I'm getting when I add something to the cart is "Caught an
exception while rendering: 'Product' object is not iterable". I'm
pretty sure that error has something to do with the cart.html, because
if I replace that line with:

return HttpResponse('You've added the product %s to the cart' % cart)

it works. The cart.html is just this:

{% for product in cart %}
Your cart: {{ product.name }} - {{product.price}}
{% endfor %}

It should work, right? I just can't figure what's causing that error.

Also, I need to be able to add more than one product to the cart, of
course, and to remove them as well. I was thinking of having the key
be the product id, and the value be the quantity. But how can I make
the id inside the product_id variable BE the name of the key? I don't
know if I'm making myself clear, english isn't my first language...
but when I call "request.session['product_id']", instead of it being
product_id, it could be the number inside it. If the product_id is 1,
it'd call "request.session['1']" or something like that. Or maybe have
multiple values for the product_id key, but then I'd have to think of
something else to work with the quantities. Maybe this isn't the
solution, but like I said, I'm pretty new to this and maybe I just
don't think like a programmer yet.

If someone has a simple solution to these problems, I'd be eternally
grateful.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---