Develop a Cart Application with Django

By Bineli Manga, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

1. Introduction

With the emergence of new data technologies and new technology products, it is now a need for every business to build its own e-commerce platform and integrate it directly into the company website. The tutorial we are presenting you now has the objective to show you how you can leverage the power of the Django framework to build a cart system.

This tutorial is the second article in our series on building an e-commerce app with Django. If you missed out the first tutorial, you can find it online here: How to Develop Web Applications on Alibaba Cloud with Django Framework

2. Scope of Our Project

2.1. Project Details

The project we are building is an e-commerce application where people can buy or sell items. For flexibility and maintainability reasons, the project hast been split into three independent apps: the Core app (Developed on the previous tutorial), the Cart app (on this tutorial) and the Payment app (on the next one).

2.2. The Cart Application

The cart application will be used to manage the buying and selling process, as well as the Cart itself with the CartItems inside it (adding, updating or removing CartItems).

The specific elements that will be managed here will be:

  • Buy and Sell options
  • Cart and CartItems of the client
  • Adding, updating and removing Products to the Cart

2.3. Setting Up the Development Environment

We will start by getting the source code of the project that is already online :

  • We clone the project using GIT

NB: After issueing this command, a folder named buyandsell will be created on the project directory containing all the source code of our project.

  • make sure all the required packages are installed (Take a look at the previous tutorial setup tools section): Python3.6, Pip, Virtualenv, libpq-dev, postgresql, postgresql-contrib
  • Install virtualenv
  • Create a venv
  • Activate the venv
  • Install the dependencies
  • Initialize the database
  • Create a super user
  • Test if everything is working well

3. Develop the Cart App

We will follow this process:

  • Initialize the app
  • Define the database models inside the file
  • Define the views that will handled requests inside
  • Define the routes that users will follow to navigate through our applications using file.
  • Define the admin interface configurations that will help the application administrator to effortlessly manage the entities stored on the database using file
  • We will then customize our templates to look prettier

3.1. Initialize the App

First, we need to create a new application called cart and add it to our config file.

This command will create a new folder named cart containing the source code of our cart application.

Adding cart app to our file

3.2. Defining the Models

As mentioned above, this app will handle the following models:

  • Cart
  • CartItem

And here is the corresponding source code:

3.3. Defining the Views

3.3.1. The Views

We have to define the views that will act as our controlelrs for managing the data presented to the users.

Here is the resulting file for the Cart and CartItem database models:

3.4. Defining the Url Routes

Here are the url routes, defined in _buyandsell/cart/urls.py_:

3.5. Define Admin Interface

Generally, when you build a web application to solve a business need of a client, you are also going to build a administrator application that will manage the data stored on the database, as well as access rights, permissions and roles. This is where Django simplifies the life of web developpers by allowing them to not worry about this tasks because it is already done by default.

In order to configure the admin interface, you have to modifiy the file:, and configure it to use our models.

Here is the resulting file:

3.6. Testing that Everything Is Working Well

Then, poen your browser to this location:

4. Conclusion

By the end of this tutorial, you should be familiar on how to start building an application with the Django framework. More precisely, you have learned what Django models, views, and templates are.

The source code of the application used for this tutorial can be found here on GitHub page:

In the next part of this series of tutorials, I will show how to develop the Cart application, in order to manage the sales of your products.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store