How to Create a Fully Functional E-commerce Website with Django

Andika Pratama
Analytics Vidhya
Published in
4 min readFeb 11, 2020

Step 3 of 5 : Make an Order Summary

https://www.shuup.com/wp-content/uploads/2017/12/python-plus-django-1.jpg

In this part tutorial we will make a summary of the order in the shopping cart

Prerequisites

  • Django Latest Version
  • Anaconda (Optional) to make Virtual Environment
  • Visual Studio Code (Optional) as Code Editor

Before starting the tutorial don’t forget to activate your Virtual Environment, and the project we created in the previous tutorial. If yo not you can download it here:

A. Models

Open core / models.py using the edit code and add some functions in class model as follows :

  1. Class Item

Nothing is changed in the Item class because the order summary is not directly related to the Item class

2. Class OrderItem

In OrderItem class we will add some additional functions as follows :

  • get_total_item_price, returns the total price value of each product item
  • get_discount_item_price, returns the total price value of each product item based on discounted prices
  • get_amount_saved, returns the value of the price saved from existing discounts
  • get_final_price, returns which function is used as a price determinant (whether using the original price or discounted price)

3. Class Order

In the Order class add a function that will calculate the total price of the order :

  • get_total_price, returns the value of the total price of all ordered product items

4. Migrate model database

Do not forget migrate your model database with the command below :

$ python manage.py migrate
$ python manage.py makemigrations

Complete models.py code can be seen in the following link:

B. Views

After manage our model, now switches to the core/views.py file. before that first place your templates directory with the following link:

or you can just just take order_summary.html and place it in the templates directory.

  1. Import
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView, DetailView, View
from django.utils import timezone
from .models import (
Item,
Order,
OrderItem
)

Import all requiment library in your views.py, in django.views.generic you can see we import the View class. for the order summary views we will use this View class

2. Create OrderSummaryView

In the OrderSummaryView class we use the Order model as the model and order_summary.html as tempalate.

3. Create reduce quantity product

In the previous tutorial we made 2 functions : add_to_cart() and remove_from_cart(). add_to_cart() make the quantiy of product adding but remove_from_cart() function will make all order removed. So we will make function that will reduce a quantity of product item and remove it if quantity equal to 0

4. Update redirect url in view

In every function in views.py, we redirected all urls to the product, but we now have an order summary and it’s good that we redirect it

// change this code
return redirect("core:product"
// to
return redirect("core:order-summary"

5. Make an user must log in to make an order

For each function add an @login_required annotation, where this will throw the user into the login page when running the function if the user has not logged in

@login_required
def add_to_cart(request, pk):
...
@login_required
def remove_from_cart(request, pk):
...
@login_required
def reduce_quantity_item(request, pk):
....

Complete views.py code can be seen in the following link:

C. Urls

After completing the view, we will now add the OrderSummaryView url and the reduce_quantity item function in the core/url.py file

  1. Import
from django.urls import path
from .views import (
remove_from_cart,
reduce_quantity_item,
add_to_cart,
ProductView,
HomeView,
OrderSummaryView
)

add all the views and functions that we made in the views.py file on import, so we just add OrderSummaryView and reduce_quantity_item from the previous file.

2. Create Path Url

Create path url to OrderSummaryView and reduce_quantity_item() function like this :

urlpatterns = [
...
path('order-summary', OrderSummaryView.as_view(),
name='order-summary'),
path('reduce-quantity-item/<pk>/', reduce_quantity_item,
name='reduce-quantity-item')
]

And now you can access the view and another function

Complete Source Code this Part :

Proceed to the next part!

I hope you’ve found the third part of this tutorial helpful. In the next session, we’re going to make Checkout form to this E-Commerce Website.

--

--

Andika Pratama
Analytics Vidhya

Fresh Graduate of Computer Science at Universitas Syiah Kuala, Software Engineer. Check my github on github.com/Andika7