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

24 Feb, 2022
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 Project using MVT in Django?
How to Create an App in 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 –  
 
In ecom> urls.py 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 > URLs.py. And for that we need to include frontend > URLs inside the ecom> URLs. 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('frontend.urls')),
]
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
Just after creating model, we should register that form into admin.py. 
from django.contrib import admin
from .models import *


admin.site.register(Product)
In frontend > views.py 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})
 Here we will set dynamic URL which can be useful to display our products. In frontend > urls.py
from django.urls import path
from . import views

urlpatterns = [

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

]
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>
  </div>
</div>
{% endfor %}
Now its time to migrate our model into database. First, we have to create migrations. For that type following code into terminal.
python manage.py makemigrations
After creating migrations type following code to apply those migrations
python manage.py migrate
To know more about makemigrations and migrate, check out – Basic App Model – Makemigrations and Migrate
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. 
After creating superuser, in terminal type,
python manage.py runserver
  to start the server and go to admin panel (http://localhost:8000/admin) and add some products to the database.
Thats it. Your basic eCommerce site is ready where you can display your products 
sooda367
gabaa406
avtarkumar719
Python Django
Python Framework
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts