Shopping carts with Django?

475 views
Skip to first unread message

iain duncan

unread,
Sep 29, 2006, 7:08:35 PM9/29/06
to django...@googlegroups.com
Hi everyone. I've done a couple of few django CMS sites now and have in
the past done a complete custom ecommerce-cms-order tracking system in
php. Now I have a client that I would like to use Django with and they
need a simple but customized shopping cart system. Wondering what folks
are using to do this in Django. Any libraries or examples out there? Any
Django middleware I should take a look at? Or is it worth gluing a
Django site to another existing cart system? Myself I tend towards the
wanting to do it myself side of things, but obviously there are limits!

Thanks
Iain

alex kessinger

unread,
Sep 29, 2006, 7:14:13 PM9/29/06
to django...@googlegroups.com
http://satchmo.python-hosting.com/

is an attempt to make an ecommerce site using django

iain duncan

unread,
Sep 29, 2006, 7:43:18 PM9/29/06
to django...@googlegroups.com
On Fri, 2006-29-09 at 12:14 -0700, alex kessinger wrote:
> http://satchmo.python-hosting.com/
>
> is an attempt to make an ecommerce site using django

Wicked cool. I'll subscribe. When do we get name something Lester or
Prez? Guess I'd have to invent it. ;)

Thanks
Iain

Oh yeah, to the Satchmo dudes, whoever you are, the about link on the
trac page is broken.

Maximillian Dornseif

unread,
Sep 29, 2006, 8:32:51 PM9/29/06
to Django users
I built one myself and it was quite easy, yust to give you an
impression of the LoC used:

root@airvent:/usr/local/web/diamond $ wc -l shop/*
shop/templates/shop/*
0 shop/__init__.py
105 shop/models.py
14 shop/urls.py
153 shop/views.py
56 shop/templates/shop/cart.html
36 shop/templates/shop/category.html
100 shop/templates/shop/checkout.html
13 shop/templates/shop/emptycart.html
21 shop/templates/shop/nocookie.html
32 shop/templates/shop/product.html
67 shop/templates/shop/shop_homepage.html
43 shop/templates/shop/thanks.html

It was relatively simple since the order processing is done in an other
(RUby on Rails) application and all the product data was already in
place from other sources. So I basically had to provide the cart and
nothing else.

Some of the more interesting view functions are:

def testcookie(request):
request.session.set_test_cookie()
return HttpResponseRedirect("../")

def addtocart(request, artnr):
if not request.session.test_cookie_worked():
return render_to_response('shop/nocookies.html', \
{}, context_instance=template.RequestContext(request))
else:
request.session.delete_test_cookie()
cart = request.session.get('cart', None) or Cart()
cart.add_item(artnr)
request.session['cart'] = cart
return HttpResponseRedirect("/shop/cart")

def cart(request):
cart = request.session.get('cart', None)
if not cart:
return render_to_response('shop/emptycart.html',
{}, context_instance=template.RequestContext(request))
else:
return render_to_response('shop/cart.html',
{'cart': cart},
context_instance=template.RequestContext(request))

def updatecart(request):
cart = request.session.get('cart', None)
if not cart:
return render_to_response('shop/emptycart.html',
{}, context_instance=template.RequestContext(request))
for k in request.POST:
if k.startswith('menge_'):
artnr = k.split('_')[1]
menge = request.POST[k]
menge = re.sub(r'[^0-9]', '', menge)
if menge == '':
menge = 0
try:
menge = int(menge)
except:
menge = 1
cart.update_item(artnr, menge)
request.session['cart'] = cart
return HttpResponseRedirect("../")

def checkout(request):
cart = request.session.get('cart', None)
manipulator = RecipientAddress.AddManipulator()
if not cart:
return render_to_response('shop/emptycart.html',
{}, context_instance=template.RequestContext(request))

if request.POST:
# If data was POSTed, try to create a new RecipientAddress.
new_data = request.POST.copy()
# Check for errors.
errors = manipulator.get_validation_errors(new_data)
if not errors:
# No errors. This means we can save the data!
manipulator.do_html2python(new_data)
new_address = manipulator.save(new_data)

request.session['oldcart'] = cart
del(request.session['cart']) # remove cart from session
request.COOKIES['hudora_kundennummer'] = /
"WC%6d" % (new_address.id)
consignment = Consignment(created_via='webshop',\
recipient=new_address)
created_by_ip = request.META.get('REMOTE_ADDR', '')
created_by_ua = request.META.get('HTTP_USER_AGENT', '')
consignment.save()
for cartitem in cart.items():
item = ConsignmentItem(consignment=consignment,
quantity=cartitem['menge'],
artnr=cartitem['artnr'],

unit_price=cartitem['einzelpreis'])
item.save()
# add shipping and handling
ConsignmentItem.objects.create(\
consignment=consignment, \
quantity=1, artnr='P0400', unit_price=4.00)

def thanks(request):
return render_to_response('shop/thanks.html',
{'cart': request.session.get('oldcart', None),
'order': request.session.get('lastorder', None),},
context_instance=template.RequestContext(request))

rikl...@gmail.com

unread,
Sep 29, 2006, 9:12:55 PM9/29/06
to Django users

Chris Moffitt

unread,
Sep 30, 2006, 1:10:11 AM9/30/06
to django...@googlegroups.com
>
> Oh yeah, to the Satchmo dudes, whoever you are, the about link on the
> trac page is broken.
>
>
>
As one of the "satchmo dudes", welcome to the group. We have a nice
foundation but there's plenty of room for improvement so we welcome your
comments.

Once you get on the list, let me know which trac page is broken - I
can't seem to find it when I browse the site.

-Chris

Chris Moffitt

unread,
Sep 30, 2006, 1:12:34 AM9/30/06
to django...@googlegroups.com
Maximillian Dornseif wrote:
> I built one myself and it was quite easy, yust to give you an
> impression of the LoC used:
>
> root@airvent:/usr/local/web/diamond $ wc -l shop/*
> shop/templates/shop/*
> 0 shop/__init__.py
> 105 shop/models.py
> 14 shop/urls.py
> 153 shop/views.py
> 56 shop/templates/shop/cart.html
> 36 shop/templates/shop/category.html
> 100 shop/templates/shop/checkout.html
> 13 shop/templates/shop/emptycart.html
> 21 shop/templates/shop/nocookie.html
> 32 shop/templates/shop/product.html
> 67 shop/templates/shop/shop_homepage.html
> 43 shop/templates/shop/thanks.html
>
>
>
Thanks for the code snippets. If you'd like to help with Satchmo, feel
free to check it out.

Is your code in an SVN repository somewhere? It would be great to see
what parts we might be able to leverage.

-Chris

iaind...@telus.net

unread,
Sep 30, 2006, 1:25:19 AM9/30/06
to django...@googlegroups.com
Quoting Chris Moffitt <ch...@moffitts.net>:

It's working again. Probably something just conking out bewteen my ISP and the host.

Iain

Maximillian Dornseif

unread,
Sep 30, 2006, 9:07:15 AM9/30/06
to Django users

Chris Moffitt wrote:
> Is your code in an SVN repository somewhere? It would be great to see
> what parts we might be able to leverage.

Unfortunately not at this time. You might know the Brooks metric.

Make it work: 1 Month
Make it installable on a generic machine: 3 Months
Document it so others can use it: 3 Months
all of the above: 9 Months

Since this is an inhouse development and we probably will never set um
another instance there is little incentive to clean it up. Then again
... wasn't that how ellington started?

Regards

Max

Maximillian Dornseif

unread,
Sep 30, 2006, 5:29:56 PM9/30/06
to Django users
Some more explanation on the design:

* the shop is for low value goods and payment is via invoice.This
allows a very lean shopping process: search for the product, click on
the spare part to add it to the cart, click on checkout button enter
your address click on commit and you are done.
* no stupid accepting terms - the customer is always right, isn't he -
, no "click here to deny not requesting to be not being on a mass
mailing list". Keeps the checkout process also lean and fast.
* stuff is added by an url like /shop/cart/add/<productid>/testcookie/
this sets the testcookie and redirects to /shop/cart/add/<productid>/
where we test for the testcookie and add the product with the id
<productid> (artnr) to the cart.

ewoudenberg

unread,
Nov 12, 2006, 6:21:02 AM11/12/06
to Django users
Hi Folks,

I took Max's helpful example and filled in the rest. I've made a Google
Code project located here: http://code.google.com/p/django-cart/

There is zero documentation other than the project home title sheet. As
Max says, writing the code is a small part of what it takes to make a
serious project. Still, I thought at least I could contribute the code.

Here is the project title sheet:

This is a simple shopping cart for the Django Web Framwork that
integrates with Google Checkout and Paypal

This Python code implements a simple shopping cart that keeps track of
orders placed on a Django-based e-commerce site. The cart is stored in
the Django Session dictionary, which is backed by the Django Session DB
table.

The shopping cart page contains 2 checkout buttons for payment
processing through either Google Checkout or Paypal. The checkout
buttons post forms that hold the appropriate data for these two systems
in hidden fields.

This is based upon some a shopping cart view.py example in
http://groups.google.com/group/django-users/browse_thread/thread/96dd3cc6b74f8a1e
by Maximillian Dornseif and on a Django/Google Checkout integration
article by Bruce Kroeze in
http://coderseye.com/2006/google-checkout-initial-impressions-from-a-developer.html

Reply all
Reply to author
Forward
0 new messages