Python Projects Rating 5 out of 5

Create an E-commerce Website with Django - Part 1

Create an E-commerce Website with Django - Part 1 Dec. 12, 2020

What is E-Commerce Website?

E-commerce Website refers to the buying and selling of goods or services using the internet, and the transfer of money and data to execute these transactions. 

Example: Amazon, Flipkart, Myntra, Paytm Mall etc...


Creating E-Commerce Website with Django

I hope all of you are familiar with Python and Django. If not then you can learn basics of Python and Django to get started.

Start a Django Project and App 

If you don't know how to start a Django project, learn from here

Save and run the following python script to create a django project automatically.

import os
import sys
import webbrowser

def launch(project, app):
    virtual = f'{project}-VE'
    setting = f'''

#Additional Required Setting
import os
os.path.join(BASE_DIR, "static")

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"'''

    urls = f'''
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)'''

    urls2 = f'''
from . import views
from django.urls import path
urlpatterns = [
    path('', views.index)
    views = '''
from django.shortcuts import render

def index(request):
    return render(request, 'index.html')

    template = '''
    <title>Wecome to CODEVER</title>
    <h1>Thanks to learn from CODEVER<h1>
    <h2>Hope you enjoyed the tutorial. Don't forget to rate and comment.</h2>

    cd = os.getcwd()
    os.system('pip install virtualenv')
    os.system(f'virtualenv {virtual}')
    os.system('call virtual\Scripts\activate')
    os.system('pip install django')
    os.system(f'django-admin startproject {project}')
    os.system(f'django-admin startapp {app}')
    os.system('python migrate')
    os.system('python createsuperuser --username admin')
    os.mkdir(os.path.join(cd, f'{virtual}/{project}/static'))
    os.chdir(os.path.join(cd, f'{virtual}/{project}/{project}'))
    with open('', 'a+',encoding='utf-8') as f:
    with open('','w',encoding='utf-8') as f:
    with open('', 'w+', encoding='utf-8') as f:
    with open('', 'w+', encoding='utf-8') as f:
    os.chdir(os.path.join(cd, f'{virtual}/{project}/templates'))
    with open('index.html','w+',encoding='utf-8') as f:
    os.system('python runserver')

project = str(input("Your Project Name: "))
app = str(input("Your App Name: "))
launch(project, app)

I have created a Django project named mycart and app named shop for tutorial.

Create a base template

Base template is common template or html code which is required in most of the pages. You can create it separately and use in any page. 

Example: Header and Footer are the same for every web page so we can write html for header and footer in base template.

<!doctype html>
<html lang="en">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <link rel="stylesheet" href="">
        background: black;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 5rem;
        width: 100vw;
        color: white;
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">Mycart</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" 
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" 
aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto mb-2 mb-lg-0">
            <li class="nav-item">
              <a class="nav-link active" aria-current="page" href="#">Home</a>
            <li class="nav-item">
              <a class="nav-link" href="#">Shop</a>
            <li class="nav-item">
                <a class="nav-link" href="#">About</a>
            <li class="nav-item">
                <a class="nav-link" href="#">Category</a>
          <form class="d-flex">
            <input class="form-control ml-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-light" type="submit">Search</button>

    <div class=container>
     {%block main%}{%endblock%}

        <div>Copyright <i class="fa fa-copyright" aria-hidden="true"></i> 2020 Codever, All Rights Reserved</div>
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"


I have created the base template with Bootstrap. 

{%block <block-name>%}{%endblock%} can be replaced in other template while using the base. 

To use the base template in another html code, Use {%extends '<filename.html>'%} at the starting of the new page.

Write your code inside {%block <block-name>%}{%endblock%}.

It will use the base file and instead of {%block <block-name>%}{%endblock%} , The html code inside this in main page will take place.

{% extends 'base.html'%}
{%block main%}
Landing Page or Index Page

Here I have used my base.html file in index.html file.

Similarly, Create another required pages such as shop, about, contact automatically by running the following python script and modify the html pages.

import webbrowser
import os 

html = '''
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <h1>This is your new HTML page. Change the file content in templates folder</h1>

def createHTML(project, app, path, view=False):
    root = os.getcwd()
        with open(f'{root}/{project}-VE/{project}/{app}/','a+',enccoding='utf-8') as f:
            if('from django.views.generic.{view}') == -1):
from django.views.generic.{view} import {view.capitalize()}View\n
class {path.capitalize}{view.capitalize}({view.capitalize()}View):
    template = '{path}.html'
        urls = f'''
        views = f'''\n
def {path}(request):
    return render(request,'{path}.html')
        with open(f'{root}/{project}-VE/{project}/{app}/', 'a+', encoding='utf-8') as f:
    os.system(f'call {project}-VE/Scripts/activate')
    with open(f'{path}.html','w+',encoding='utf-8') as f:
    with open('','r+',encoding='utf-8') as f:
        _ =']',urls)
    with open('', 'w', encoding='utf-8') as f:
    os.chdir(os.path.join(root, f'{project}-VE/{project}'))'{path}')
    os.system('python runserver')

project = input('Project: ')
app = input('App: ')
page = input('Page: ')
createHTML(project, app, page)

In this tutorial, You have created the basic E-commerce templates. In next tutorial, You will create required models such as Profiles, Products and Reviews etc.

Don't forget to rate and comment.

Rate this post