How to make an e-commerce website in Django

Overview

Django is the most common and popular framework used by renowned companies like Instagram, Spotify, YouTube, etc. Django not only provides ease of use, but also provides versatility, security, flexibility, and diverse features that are important for any website.

How can we make an e-commerce website using Django?

This whole process is divided into the following steps:

  1. Creating a virtual environment
  2. Installing Django
  3. Creating a project
  4. Creating the first application inside the project

Making an e-commerce website

First of all, we need to make sure that we have the latest version of Python installed. Then, we need to create a virtual environment.

Creating a virtual environment

To create a virtual environment, we will use the following commands:


1. python –m pip install virtualenv
2. virtualenv env

The first command will install the virtual environment and the second command will create it. Then, we will create a folder named ‘env’ that we can enter using the source env/bin/activate command.

Installing Django

Similarly, we can install Django using the following commands:


1. python –m pip install Django
2. python –m Django version 

Here, the first command will install Django and the second command will verify the successful installation of Django by showing the Django version.

Creating a project

To create the website, we will do what is mentioned below:

Since we have already created the virtual environment, we will just move the virtual environment folder named ‘env’ inside and run the following command:


django-admin startproject Ecommerce_Website

Here, Ecommerce_Website is the name – it will build up the structure of the website and provide the manage.py, settings.py, urls.py, and wsgi.py files and a directory with the same name as Ecommerce_Website.

Creating the first application inside the project

By entering in this directory, we will create the website (application) using the following command:


Python manage.py startapp EcommerceSite

Here, the directory will be created with the name EcommerceSite. This directory will contain the admin.py, apps.py, models.py, test.py, and views.py files. After making a Django website, we are now ready to give it a test run. We will run it using the following command:


python manage.py runserver

Now, if we go to the [http://127.0.0.1:8000/] link, we can check whether or not the website is running.

Note: The default IP address to access a Django project is http://127.0.0.1, while the default port is 8000.

Models

Since we have some files in the EcommerceSite directory, we can create product models for our e-commerce website in models.py file. For example:


class Product(models.Model):
    name_of_product = models.CharField(max_length=150)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    details = models.TextField()
    manufactured_by= models.TextFiled()
    quantity_left_in_stock = models.IntegerField()
    quantity_sold = models.IntegerField()
    product_reviews = models.CharField(max_length=200, null=True)
    image = models.CharField(max_length=5000, null=True, blank=True)


Similarly, we can add different product models of our choice and modify the attributes according to the requirements.

Django migrations

In case of any changes or updates in the data, we need to propagate these changes for keeping that change. For this change, we need to carry out database migrations by using the terminal and entering the following commands:


1. manage.py makemigrations <app_name>
2. manage.py migrate

Here, the first command will generate a migration and the second command will apply that to the database as required.

Templating

The final step is templating, for which we have to go to the “templates” folder, which will be available in the first folder that we named Ecommerce_Website.

We can create the template for the display according to the needs of our website. We can also use a pre-existing template with suitable modifications and set it up according to the requirements of our Django e-commerce website.

Free Resources