Learn how to create an E-Commerce website with Python Django by following this easy, step-by-step guide. Our Ecommerce team ensures your Django store runs efficiently with enhanced security and scalability.
Creating an e-commerce website is one of the best ways to explore Django’s potential in real-world web development. Django, a high-level Python framework, provides all the tools needed to build dynamic, data-driven applications with authentication, database management, and scalability.
Today, you’ll learn how to build a basic e-commerce application using Django. The project will include essential features such as product display, cart management, user authentication, and checkout functionality.

This Django project will allow customers to:
- Browse and view products
- Add or remove products from a shopping cart
- Specify quantities
- Place an order and proceed to payment
Furthermore, each user will have a secure login system to ensure that only authorized customers can access their accounts.
Overview
Choose the Right Technology
Selecting an appropriate framework is crucial in e-commerce development. Django is a preferred choice due to its ability to handle scalability, security, and feature enhancement effectively.
As your business grows, Django’s modular structure allows your website to handle high traffic and large data volumes without slowing down.
Django includes built-in protection against common web vulnerabilities such as SQL injection, CSRF, and XSS attacks, making it safe for handling transactions and personal user information.
The framework allows quick integration of advanced features like personalized recommendations, live inventory tracking, and optimized checkout experiences. To build a resilient online business, reviewing ecommerce readiness for economic uncertainty can provide valuable insights into strengthening operations and preparing for market changes.
Technologies Used
- Frontend: HTML, CSS, Bootstrap
- Backend: Python, Django
To install Django, run the following command in your terminal:
pip install django
Steps to Create an E-commerce Website with Django
Step 1: Start a New Django Project
Begin by creating a new project and an app inside it:
django-admin startproject OnlineShopping
cd OnlineShopping
django-admin startapp website
This structure helps keep the core project and app logic organized.
Step 2: Create Models
Models define the structure of your database tables. Open `models.py` in your app and add the following:
from django.db import models
from django.contrib.auth.models import User
class Customer(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
image = models.ImageField(null=True, blank=True)
description = models.CharField(max_length=400, null=True)
def __str__(self):
return self.name
class Order(models.Model):
product = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
date_ordered = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=20, default='PENDING')
def __str__(self):
return f"Order of {self.product}"
Apply these changes with:
python manage.py makemigrations
python manage.py migrate
These models represent customers, products, and orders, forming the backbone of the ecommerce system.
Looking for end-to-end eCommerce support?

Step 3: Create Forms
In `forms.py`, define forms to handle product creation, order placement, and user registration:
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import *
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'password1', 'password2']
class ProductForm(ModelForm):
class Meta:
model = Product
fields = '__all__'
class OrderForm(ModelForm):
class Meta:
model = Order
fields = '__all__'
exclude = ['status']
class CustomerForm(ModelForm):
class Meta:
model = Customer
fields = '__all__'
exclude = ['user']
Now, these forms will help manage CRUD operations for products, customers, and orders.
Step 4: Register Models in Admin Panel
In `admin.py`, register the models so you can manage them via the Django admin dashboard.
from django.contrib import admin
from .models import *
admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Order)
Create a superuser to access the admin area:
python manage.py createsuperuser
Step 5: Configure Static Files
In `settings.py`, add the following lines to manage static and media files:
import os
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Step 6: Define URL Routes
Next, add your view paths in `urls.py`:
from django.contrib import admin
from django.urls import path
from website import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('add-product/', views.add_product, name='add_product'),
path('place-order//', views.place_order, name='place_order'),
path('register/', views.register_page, name='register'),
path('login/', views.login_page, name='login'),
path('logout/', views.logout_page, name='logout'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Step 7: Create Views
In `views.py`, define logic for rendering pages and handling user interactions:
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout, authenticate
from .forms import *
def home(request):
products = Product.objects.all()
return render(request, 'website/home.html', {'products': products})
def add_product(request):
form = ProductForm()
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home')
return render(request, 'website/add_product.html', {'form': form})
def place_order(request, id):
form = OrderForm()
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
form.save()
return redirect('home')
return render(request, 'website/place_order.html', {'form': form})
def register_page(request):
form = CreateUserForm()
customer_form = CustomerForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
customer_form = CustomerForm(request.POST)
if form.is_valid() and customer_form.is_valid():
user = form.save()
customer = customer_form.save(commit=False)
customer.user = user
customer.save()
return redirect('login')
return render(request, 'website/register.html', {'form': form, 'customer_form': customer_form})
def login_page(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user:
login(request, user)
return redirect('home')
return render(request, 'website/login.html')
def logout_page(request):
logout(request)
return redirect('home')
Step 8: Design Templates
Create your templates folder (`website/templates/website/`) and add the following HTML files:
- home.html – Display products
- add_product.html– Add new product
- place_order.html– Order form
- register.html– User registration
- login.html– User login
- navbar.html– Common navigation bar
Furthermore, you can use Bootstrap to make the design clean and responsive.
Example (`home.html`):
{% extends 'website/dependencies.html' %}
{% block content %}
<div class="container">
<h2>Available Products</h2>
<div class="card-columns">
{% for p in products %}
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="{{ p.image.url }}" alt="Product image">
<div class="card-body">
<h5 class="card-title">{{ p.name }}</h5>
<p class="card-text">{{ p.description }}</p>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
Testing the Project
Run your Django server using:
python manage.py runserver
Then, open your browser and visit: http://127.0.0.1:8000/
You’ll see the home page displaying your products, with working login, registration, and cart functionality.
Conclusion
In short, you’ve just built a basic Django e-commerce website with essential features such as product listing, user registration and login, order placement and cart management.
This foundation can be expanded to include payment integration, order tracking, inventory management, and more advanced functionality.
You can easily create an E-Commerce website with Python Django for real-world use.
