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

Step 2 of 5: Make an Order

Andika Pratama
Analytics Vidhya
4 min readFeb 11, 2020

--

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

Prerequisites

  • Django Latest Version
  • Anaconda (Optional) to make Virtual Environment
  • Visual Studio Code (Optional) ad 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. Create Databases in Models

Before we start to make an order. the first thing to do is create a database that will store order data

For starters we will make 3 data tables namely:
- Item will store product data
- OrderItem will store product data that you want to order
- Order will store order information

To create a database in Django framework, we will use models.py file which is in the core directory, open file with your code editor and proceed with this tutorial.

  1. Import

2. Add Item model

In the item model you will see there are 3 additional functions, these functions include:

  • get_absolute_url will return url from product
  • get_add_to_cart_url will return url to function add item to cart in views.py file we will make
  • get_remove_from_cart_url will return url to function remove item from cart in views.py file we will make

3. Add OrderItem model

OrderItem stores data of the product you want to order and the amount of the product

4. Add Order Model

The Order model will store detailed information of the orders made, but in this part of the tutorial we will not display complete order information, we will add another field in the next part.

5. Register database to admin

open core/admin.py file witch code editor and fill with the code below :

That will register your database on the admin page. You can check it on the admin page later

6 . Migrate model database

Migrate your model database with the command below :

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

B. Managing Views

We have created a database and now it is our turn to manage and manage our views. We will create 2 views that is HomeView and ProductView and 2 function that is add_to_cart() and remove_from_cart(). to continue open core/views.py and continue with the following tutorial

  1. Import

Import all the model classes that we have previously created into views.py

2. Add HomeView

In the previous tutorial article, we made a home function to display a view. delete the home function and change it as Class View this :

We use the Item model as a home model and home.html as template view. home.html can be found at templates directory

3. Add ProductView

We use the Item model as a home model and product.html as template view. product.html can be found at templates directory

4. add_to_cart() function

This function will add your product to OrderItem database and add detail order to Order database

5. remove_from_cart() function

This function will remove all your product from OrderItem database and remove detail order from Order database

6. Update your templates directory

In templates directory we make some changes. So update your templates directory from the following link :

C. Managing Urls

After the view is finished, then the url will be managed where the view will be displayed in the browser. Open your core/urls.py file and fill with the code below :

Import all view classes and functions and add the url for each view.

D. Demo Aplications

  1. Run your server with the command below :

2. open http://127.0.0.1:8000/ and choose one product, if you do not have a product you can adding in admin page

Item Database

3. Click Add to cart button in product page and you will see the product will add to your OrderItem and Order database

Order Database

4. Click remove from cart button to remove your current order. it will delete order item from database Order and OrderItem

To this end the function to create an order has been implemented

Complete Source Code this Part :

Proceed to the next part!

I hope you’ve found the second part of this tutorial helpful. In the next session, we’re going to make an Order Summary in our shopping cart.

--

--

Andika Pratama
Analytics Vidhya

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