Starting with Django eCommerce website with django-oscar

Image for post
Image for post

Django is relatively easy framework to learn and can grow on you quickly. Sites like Instagram, Spotify, YouTube, The Washington Post, Bitbucket, Dropbox, Mozilla, Pinterest, Quora, Disqus, NASA Offical Website etc were developed using Django Python Web framework. Source

Django-oscar is a eCommerce framework in django designed to build domain-driven sites means it solves the problem domain of complex eCommerce systems. It is structured in a way that its core functionality can be customized to your needs and it allows a wide range of eCommerce requirements to be handled easily. It solves large scale Business-to-Customer(B2C) to complex Business-to-Business(B2B) sites and gives domain specific business logic and solutions.

Let’s look at its installation so that you can easily start django eCommerce project with django-oscar today.

First of all you need is to create a virtual environment. It can be done without virtual environment but I suggest you to create and let why be question for another day.

py -m venv oscar

py or python3 according to you Python setup. This creates a new environment oscar for your project. And activate your environment with:

On Windows, run:

source oscar/scripts/activate

In Mac or Unix, run:

source oscar/bin/activate 

Now, you need to install django-oscar via python package manager, pip, in virtual environment.

pip install django-oscar
Image for post
Image for post

After django-oscar installed successfully. Now run following command to install django project. Let’s call our project myshop.

django-admin.py startproject myshop

Go into the folder where manage.py is located. In our case, run:

cd myshop

Now wait and lets edit out myshop.myshop.settings.py file. Paste the following code at the top of settings.py file.

from oscar.defaults import *
from oscar import OSCAR_MAIN_TEMPLATE_DIR
from oscar import get_core_apps
# Path helper
location = lambda x: os.path.join(
os.path.dirname(os.path.realpath(__file__)), x)

Now modify your TEMPLATES to include the main Oscar template directory and add the extra context processors. You can replace the whole block into following code.

TEMPLATES = [
{
'BACKEND':'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
OSCAR_MAIN_TEMPLATE_DIR
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',

'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
],
},
},
]

Next, modify INSTALLED_APPS add django.contrib.sites, django.contrib.flatpages, and widget_tweaks and append Oscar’s core apps and set SITE_IDto 1 as below:

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',

'widget_tweaks',
] + get_core_apps()

SITE_ID = 1

Next, Add 'oscar.apps.basket.middleware.BasketMiddleware', 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' in the MIDDLEWARE

'oscar.apps.basket.middleware.BasketMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',

And add the authenciation backends and haystack search for default search as below:

AUTHENTICATION_BACKENDS = (
'oscar.apps.customer.auth_backends.EmailBackend',
'django.contrib.auth.backends.ModelBackend',
)
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
}

Now, you can go to terminal with manage.py in current directory. And run:

python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

You should see the following:

Image for post
Image for post

Now, go into /admin to enter the admin username, password you set earlier and you will see all the oscar componets ready to roll.

Image for post
Image for post

Okay this is the end. You can now start to playing around with it to create your domain driven beautiful website.

I hope this helps someone. Feel free to clap for appreciation and comment for problems. I will try to help as much as I can.

Thank you. Happy Hacking.

Written by

I design, develop web components and systems and write about them.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store