Using Sessions to display the contents of a shopping cart

495 views
Skip to first unread message

Greg

unread,
Jul 19, 2007, 11:37:42 AM7/19/07
to Django users
I have the following view:

def showcart(request, style_id, choice_id):
s = Style.objects.get(id=style_id)
c = Choice.objects.get(id=choice_id)
x = request.session ?? # I want to add s and c to my session?
return render_to_response('show_test.html', {'mychoice': x})

//////

Everytime the user adds something to their shopping cart then this
view will be accessed. I want the view to store the two parameters (s
and c) into my request.session so that the data in the session can be
showed when the website visitor checks out. However, I'm not sure how
to add these parameters to my session variable. I'm also not sure how
to display all the session data.

Thanks for any help

Jeremy Dunck

unread,
Jul 19, 2007, 11:45:46 AM7/19/07
to django...@googlegroups.com
On 7/19/07, Greg <gms...@hotmail.com> wrote:
> x = request.session ?? # I want to add s and c to my session?

cart = request.session.get('cart', [])
cart.append({'style':s,'choice'c})

request.session['cart']=cart

But, fundamentally, putting cart info in session is a bad idea; people
switch computers (and thus cookies; sessions) fairly often. Also,
people delete cookies for good reasons. I'd be severely pissed if my
amazon wishlist went away when I deleted cookies.

> I'm also not sure how
> to display all the session data.

http://www.djangoproject.com/documentation/authentication/#authentication-data-in-templates
http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext
http://www.djangoproject.com/weblog/2005/sep/24/newshortcuts/

Lutz Steinborn

unread,
Jul 19, 2007, 12:40:08 PM7/19/07
to django...@googlegroups.com
Hi Jeremy,

On Thu, 19 Jul 2007 10:45:46 -0500
"Jeremy Dunck" <jdu...@gmail.com> wrote:

>
> On 7/19/07, Greg <gms...@hotmail.com> wrote:
> > x = request.session ?? # I want to add s and c to my session?
>
> cart = request.session.get('cart', [])
> cart.append({'style':s,'choice'c})
>
> request.session['cart']=cart
>
> But, fundamentally, putting cart info in session is a bad idea; people
> switch computers (and thus cookies; sessions) fairly often. Also,
> people delete cookies for good reasons. I'd be severely pissed if my
> amazon wishlist went away when I deleted cookies.

but was is the solution if the user is not logged in ?
At this point your only chance is a session bound cookie or I'm wrong ?

Kindly regards
Lutz Steinborn

Greg

unread,
Jul 19, 2007, 3:10:54 PM7/19/07
to Django users
Lutz,
Yea I don't plan on having the user login to purchase a product from
me. So I guess Session's will work if that is the case. It looks
like I will have to place a cookie on the visitor's computer using
set_test_cookie() and test_cookie_worked() method's so that I'm able
to tell the difference between two users that are adding products to
the cart at the same time. Does anybody know of any problems with
this solution?

On Jul 19, 11:40 am, Lutz Steinborn <l.steinb...@4c-ag.de> wrote:
> Hi Jeremy,
>
> On Thu, 19 Jul 2007 10:45:46 -0500
>
> "Jeremy Dunck" <jdu...@gmail.com> wrote:
>

Jeremy Dunck

unread,
Jul 19, 2007, 5:09:24 PM7/19/07
to django...@googlegroups.com
On 7/19/07, Greg <gms...@hotmail.com> wrote:
> So I guess Session's will work if that is the case. It looks
> like I will have to place a cookie on the visitor's computer using
> set_test_cookie() and test_cookie_worked() method's so that I'm able
> to tell the difference between two users that are adding products to
> the cart at the same time. Does anybody know of any problems with
> this solution?

No, don't do that. The session framework already does that for you.

If you use request.session, and the browser accepts cookies, you'll
magically have values in request.session as set on previous requests.

http://www.djangoproject.com/documentation/sessions/

Chris Moffitt

unread,
Jul 19, 2007, 5:50:24 PM7/19/07
to django...@googlegroups.com
The way we handle it with Satchmo is to have a cart object and store that id in the session.  It's pretty simple and does not require a user to be logged in.

-Chris

Reply all
Reply to author
Forward
0 new messages