Open In App

Django project – Creating a Basic E-commerce Website for Displaying Products

Last Updated : 24 Feb, 2022
Like Article

Project Title – Basic Ecommerce Website using Django 

Django is a powerful framework based on python. Here we will see how to create a basic e-commerce website in Django. This project includes storing products in the database and show them on the website. 

Refer to the following articles to check how to create a project and an app in Django.

How to Create a Basic Ecommerce Website using Django?

Now when you have successfully installed Django. Create a new project – 

django-admin startproject ecom 

Now create a new app called frontend inside the ecom project. Now we have 1 project and 1 app inside of that project.]

django-admin startapp frontend

Directory structure – 


Creating URLs – 

In ecom> add the following lines. This file handles main project URLs. But we don’t want to disturb it, so we will do our work in frontend > And for that we need to include frontend > URLs inside the ecom> URLs. 

urlpatterns = [
    path('', include('frontend.urls')),

Creating Models – 

Add a product model here.

class Product(models.Model):
    productname = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    description = models.TextField()
    image = models.CharField(max_length=5000, null=True, blank=True)

To know more about Django models, check out – Django Models

Register Models into admin – 

Just after creating model, we should register that form into

from django.contrib import admin
from .models import *

Creating Views for Displaying Products – 

In frontend > we write a function to get and display products from our database. 

from django.shortcuts import render
from .models import *

def products(request):
    products = Product.objects.all()
    return render(request, 'products.html', {'products':products})

Creating Url for products – 

 Here we will set dynamic URL which can be useful to display our products. In frontend >

from django.urls import path
from . import views

urlpatterns = [

    path('products/', views.products, name='products'),


Creating Template – 

Creating template depends upon how you want to display products on website. For displaying products we have shared a simple code. Add it in frontend > templates > products.html

{% for product in products %}
<div class="card" style="width: 18rem;">
  <img class="card-img-top" src="{{ product.image }}" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">{{ product.productname }}</h5>
    <p class="card-text">{{ product.description }}</p>
    <p class="card-text">Price - {{ product.price }}</p>
    <a href="#" class="btn btn-primary">Buy Now</a>
{% endfor %}

Makemigrations and Migrate –

Now its time to migrate our model into database. First, we have to create migrations. For that type following code into terminal.

python makemigrations

After creating migrations type following code to apply those migrations

python migrate

To know more about makemigrations and migrate, check out – Basic App Model – Makemigrations and Migrate

Creating Superuser – 

Now, create Django superuser for handling admin stuff. Type following command in terminal

django-admin createsuperuser

Then it will ask for username, email and password. 

Run the app 

After creating superuser, in terminal type,

python runserver

  to start the server and go to admin panel (http://localhost:8000/admin) and add some products to the database.

Output – 

Thats it. Your basic eCommerce site is ready where you can display your products

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads