Django And eCommerce


I like Python and want to use it for eCommerce, so I went through the well known products and settled on Oscar as my demo. It took a while to set up, but was pretty full featured. Python and Django still have to compete with short busses like wordpress and dump trucks like Magento, but I think these tools are ready for me to start pushing.


Let me say this took me almost 2 days to get through and evaluate, which I’m used to having a demo site up in minutes. After having gone through, I think this could be launched in half an hour. Below are my notes of the process.


I was contacted by a business partner to see if we could kick off a project to distribute some substances that newly became legal here in CA. I thought I could spin up an ecommerce app in a couple hours and be taking payments in a week. I wanted to see how fast I could do this with Django. Most my previous eCommerce deployments (many) were in PHP and many of them were in magento. Had a few in ASP, Perl, (Websphere) Java. I’m over that pain.

Online retail - E-commerce - as an industry, is very mature. It requires a TON of flexibility and features to compete. There isn’t a lot of waste.


I will append this by saying a few things were already installed like PostgreSQL and Python VirtualenvWrapper.

Here are a couple details about my stack.

  • Local development: Mac OS X El Cap
  • Bitbucket (it’s free for private accounts)
  • Python VirtualenvWrapper
  • Django 1.10.5
  • PostgreSQL 9.5
  • Django-Oscar

Let’s Begin

Local Setup

Setup PostgreSQL

I have installed PostgreSQL locally already. Please see the documentation for more information.

PostgreSQL 9.5.3 on x86_64-apple-darwin15.4.0, compiled by Apple LLVM version 7.3.0 (clang-703.0.31), 64-bit

Create a database for yourself. You will probably be hitting this locally with socket.

createdb [project_slug]

Virtual Environment - Python

Python 2.7

Yes I know.

I’m still not on P3. Leave me alone, gosh. There is always some library that causes me pain. I make it future-proof as much as possible though.

mkvirtualenv yourapp

CD into a directory beneath your source location. I always do /project/source/*

Install cookiecutter-django

I have used this cookiecutter before so I stick with what I know.


Go through the questions.

Add the new files to the remote repository and push it.

git init
git add .
git commit -m "first commit"
git remote add origin
git push -u origin master

Django Setup

I decided to try out Django Oscar. I figure it is the most popular. And also hoping it isn’t the most bloated.

Add oscar to the bottom of the requirements file.

vim requirements/base.txt
-e git+

There is a problem with oscar and Django 1.10 so pull the latest from master. I also had to modify the source to get the middleware to work as well. Small enough problems that I didn’t totally abandon the project at this point. I entered a code fix + pull request so hopefully it is fixed. There was also a problem with the pycountry change as listed below.

Now pip install everything.

pip install -r requirements/local.txt

This should bring in everything you need.

Copy the environment template file.

cp env.example config/settings/.env

Run the manage command to confirm.


Set Up Oscar

in yourapp/config/settings/

Add ‘oscar’ to the third party installed applications in the settings file.

According to the documentation update the settings for the templates.

# Import all the oscar items
from oscar import OSCAR_MAIN_TEMPLATE_DIR
from oscar import get_core_apps
from oscar.defaults import *

    # Default Django apps:

    # Useful template tags:
    # 'django.contrib.humanize',

    # Admin

    'crispy_forms',  # Form layouts
    'allauth',  # registration
    'allauth.account',  # registration
    'allauth.socialaccount',  # registration

    # the oscar apps
) + tuple(get_core_apps())

        # See:
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # See:
        'DIRS': [
        'OPTIONS': {
            # See:
            'debug': DEBUG,
            # See:
            'loaders': [
            # See:
            'context_processors': [
                # Your stuff: custom template context processors go here

This allows the users to log in with email as well as username, but I don’t know if I will use that.


At the bottom set the haystack settings.

    'default': {
        'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',

Set the URLs

Here is my url file.

from import application

urlpatterns = [
    # url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name='home'),
    url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name='about'),

    # Django Admin, use {% url 'admin:index' %}

    # User management
    url(r'^users/', include('yourapp.users.urls', namespace='users')),
    url(r'^accounts/', include('allauth.urls')),

    # Your stuff: custom urls includes go here
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'', include(application.urls)),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:

Build Schema

If the database is created and you set up everything properly you should be able to build the DB schema now.

./ migrate

Start Local

Kick off the local server.

./ runserver_plus

If everything works properly you should be able to go to http://localhost:8000/ and see your new project running.

Store Setup

This wasn’t quite enough for me so I needed to add some products into the application really quickly to see the catalogue.

Install Shipping Countries

Pycountry should have been installed already, and it was suggested to load all the countries in the oscar documentation.

./ oscar_populate_countries --no-shipping

This threw an attribute error. At this point I am thinking this application may be a bit fragile. There is a pull request for it so I just manually made the change and it worked.

I’m annoyed at pycountry for a menial change like that. Or provide a better SDK.

Where Templates Are?

I realized that the templates were not seen even though they were in the path. I just copied them from the oscar sandbox example so that I had more to go off of.


I also copied over all the static content into the media directory.

/src/oscar/static/oscar/img > yourapp/static


Make sure you created a superuser account.

Login and travel to the dashboard:

Product Setup

You need to have a category and a shipper. That is easy enough to set up. I added a couple products and viewed them on the

Product Page



Code Structure

This looked fine to me. I was able to find my way around very easily compared to the likes of Magento, IBM, SAP. Thank goodness. That is stuff of nightmares.


The framework is important to me. I like to see proper recognizable patterns. There were red flags with the model class “loader” paradigm when I expected to see Django objects. I think that is unnecessary with a framework that has good bootstrapping functionality such as Django. Maybe someone can explain where my thinking is in error.

No REST for me

I was disappointed with how hard it was to find information about oscar and an integration with a REST layer. Most sites MUST support mobile. And probably should support mobile FIRST. The old days of a monolithic website stack are kinda over. Especially with javascript running rampage over the facades of the interwebs.

Yes, I know there is django oscar api - It needs more thorough documentation, and matrix of supported features would be nice. Overall it looks promising.


The application felt kinda slow. Maybe it can be optimized, but for a starter, I felt like it should be more responsive. The feel of other apps was a little better. With ecommerce the most important thing is checkout flow followed by performance and product data. Don’t skimp on ANY of those things. Let the design languish if you have budget for a performance hacker. I’ve launched large ecommerce sites and this is what killed most of them once traffic rolled in.

Will it Stay

I still have to look through Saleor and Cartrige on Mezzanine which were very appealing. I also wanted to have a hand at setting up an ecommerce app in Go, but the lack of templating and abstraction that python is good at was a knock down.