Managing Bank ATM Locations Using PostgreSQL and Django

In this chapter, you will learn how to install and configure PostgreSQL so that you can use it with a Django application. First, we will install the required software. Then, we will create database credentials for our application. Finally, we will start and configure a new Django project that uses PostgreSQL as the backend.

The project of this chapter will show you how to implement a banking system to manage ATM locations within a typical city through the Django environment on the PostgreSQL 12 Relational Database Service (RDSfrom Amazon Web Services (AWS), which we explored in Chapter 3Using PostgreSQL and NodeJS for Banking Transactions.

In this chapter, we will cover the following topics:

  • Setting up a Django project
  • Django database settings with PostgreSQL
  • Database models in Django
  • Migrating the database
  • Understanding the Django user interface  admin, views, templates, and URLs

Technical requirements

This chapter will take developers between 10 and 12 hours to develop an ATM Django app.

The code files for this chapter are available at https://github.com/PacktPublishing/Developing-Modern-Database-Applications-with-PostgreSQL/tree/master/Chapter04.

Setting up a Django project

We will use the same CentOS 7 EC2 instance in our AWS account from Chapter 3, Using PostgreSQL and NodeJS for Banking Transactions, to deploy our Django project. So we put our EC2 instance with the built-in user for our current CentOS AMI user:

[centos@ip-172-31-95-213 ~]$ sudo su
[root@ip-172-31-95-213 centos]# cd /usr/local/src/

Following this sudo command, the rest of our statements are going to be executed under the user root.

Installing Python 3

By default, CentOS 7 comes with Python 2.7.5, and Python 2.7.5 is a crucially built-in part of the CentOS basesystem. However, we intend to create our project with Python version 3.7, so we will now install Python 3.7 on CentOS 7.

At the time of writing this book, Python 3.7 is the current version, and its installation requires the GCC compiler on our ec2 instance. Please refer to the following steps to install the prerequisites for Python before installing it:

  1. First, we enable SCL by installing the CentOS SCL release file. This is included in the CentOS Extras repository:
[root@ip-172-31-95-213 src]# yum install gcc openssl-devel bzip2-devel libffi-devel
  1. After executing the preceding command, the console will look similar to Figure 4.1. Please answer y and you should see something similar to the following screenshot:

Figure 4.1 – Installing the CentOS SCL release file
  1. Next, download Python 3.7 using the following command from Python's official website:
[root@ip-172-31-95-213 src]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
[root@ip-172-31-95-213 src]# tar xzf Python-3.7.3.tgz
[root@ip-172-31-95-213 src]# cd Python-3.7.3
[root@ip-172-31-95-213 Python-3.7.3]# ./configure --enable-optimizations
[root@ip-172-31-95-213 Python-3.7.3]# make altinstall
  1. Now remove the downloaded source archive file from your system:
[root@ip-172-31-95-213 Python-3.7.3]# rm /usr/local/src/Python-3.7.3.tgz
rm: remove regular file ‘/usr/local/src/Python-3.7.3.tgz'? y
[root@ip-172-31-95-213 Python-3.7.3]#
  1. As shown in the following code block, check the version of Python that has been installed. Use the python3.7 command instead of just python:
[root@ip-172-31-95-213 Python-3.7.3]# python3.7 -V
Python 3.7.3
[root@ip-172-31-95-213 Python-3.7.3]# cd ..
[root@ip-172-31-95-213 src]#

Now that we have successfully installed Python 3.7, we can move on to create a virtual environment for our Django application.

Creating a virtual environment

One of the biggest difficulties with Django developers is that a project created in one Django version is not compatible with another project in a different Django version. A virtual environment is a Python tool that will create a new environment. Each virtual environment is project-specific and isolated with its own set of packages, settings, and dependencies. Therefore, a virtual environment is one solution to resolve the version difficulty of Django projects. The steps to create a virtual environment are as follows:

  1. First, create a separate folder for our Django work, as follows:
[root@ip-172-31-95-213 src]# mkdir django_app
[root@ip-172-31-95-213 src]# cd django_app
[root@ip-172-31-95-213 django_app]
  1. Next, run the following command to create a new virtual environment:
[root@ip-172-31-95-213 django_app]# pip python3.7 -m venv venv

Following the execution of the preceding command, a directory called venv will be created. It contains a copy of the Python binary, the pip package manager, the standard Python library, and other supporting files. You can set the name of the virtual environment to any name of your choice:

[root@ip-172-31-95-213 django_app]# ls
venv
  1. The virtual environment will start after you run the following script:
[root@ip-172-31-95-213 django_app]# source venv/bin/activate
  1. After the virtual environment has been activated, the virtual environment's bin directory will appear at the start of the $PATH variable. The shell's prompt will display the name of the virtual environment that you're currently using. In our case, that is venv. Let's take a look at the following command:
(venv) [root@ip-172-31-95-213 django_app]# echo $PATH

After executing the preceding command, if your console looks similar to the following screenshot, this means that you have successfully created your virtual environment:

Figure 4.2 – Activating the virtual environment

To exit the virtual environment, you can just type in deactivate, which sets the system back to what it was earlier.

Installing Django

The virtual environment has now been activated. Therefore, we can use the Python package manager, pip, to install Django. Perform the following steps:

  1. Install Django using the following command:
(venv) [root@ip-172-31-95-213 django_app]# pip3 install django

The following screenshot shows the installation output on our console:

Figure 4.3 – Installing Django
  1.  The following command will help you to verify the installation process. It will print the Django version:
(venv) [root@ip-172-31-95-213 django_app]# python3.7 -m django --version
2.2.6
  1. Surprisingly, the installation of Django is very simple and quick for developers. Version 2.2.6 of Django requires SQLite versions later than SQLite 3.8.3; hence, you will have to install SQLite 3.29.0 as follows:
(venv) [root@ip-172-31-95-213 django_app]# cd ..
(venv) [root@ip-172-31-95-213 src]# wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
(venv) [root@ip-172-31-95-213 src]# tar zxvf sqlite-autoconf-3290000.tar.gz
(venv) [root@ip-172-31-95-213 src]# cd sqlite-autoconf-3290000
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# ./configure --prefix=$HOME/opt/sqlite
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# make && make install
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# export PATH=$HOME/opt/sqlite/bin:$PATH
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# export LD_LIBRARY_PATH=$HOME/opt/sqlite/lib
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# export LD_RUN_PATH=$HOME/opt/sqlite/lib
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# source ~/.bash_profile
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# sqlite3 --version
3.29.0 2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6
(venv) [root@ip-172-31-95-213 sqlite-autoconf-3290000]# cd ..
(venv) [root@ip-172-31-95-213 src]# rm sqlite-autoconf-3290000.tar.gz
rm: remove regular file ‘sqlite-autoconf-3290000.tar.gz'? y
(venv) [root@ip-172-31-95-213 src]# cd django_app
(venv) [root@ip-172-31-95-213 django_app]#

Now you can continue to use Django version 2.2.6 without any problems.

Creating a Django project

In order to create a Django project, we have to autogenerate some code that establishes a Django project that is a collection of settings, including the database configurations, Django-specific options, and application-specific settings. Perform the following steps:

  1. We will start by running the following command:
(venv) [root@ip-172-31-95-213 django_app]# django-admin startproject atmproject

The preceding command will create an atmproject directory in your current directory with the following structure:

(venv) [root@ip-172-31-95-213 django_app]# yum install tree -y
(venv) [root@ip-172-31-95-213 django_app]# tree atmproject
atmproject
├── atmproject
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
  1. You have successfully created a new Django project, as shown in the following screenshot:

Figure 4.4 – The Django project structure

The preceding project, that is, our environment, has been set up. In the next section, we will learn how to create an app. 

Creating the ATM app

Each app that we write in Django consists of a Python package, and we can use Django to automatically generate the basic app directory structure. One Django project can include many apps and configurations for a particular website, and an app can be included inside multiple projects. 

 Let's start by creating our own ATM app with the help of the following command:

(venv) [root@ip-172-31-95-213 django_app]# cd atmproject
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py startapp atmapp

The following is the structure of an app:

(venv) [root@ip-172-31-95-213 atmproject]# tree atmapp
atmapp
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py

In the console, the preceding structure will appear as follows:

Figure 4.5 – The Django app structure

Now that we have generated our Django project and ATM app structures, next, we are going to arrange our PostgreSQL RDS settings for this project.

Django database settings with PostgreSQL

By default, Django uses the SQLite database. For production applications, you can use the PostgreSQL, MariaDB, Oracle, or MySQL databases. For this book, it will be PostgreSQL. Perform the following steps:

  1. The virtual environment is active; now, we will install the psycopg2 PostgreSQL adaptor with the help of the following command:
(venv) [root@ip-172-31-95-213 atmproject]# pip3 install psycopg2-binary

After the preceding command has been executed, the console should appear as follows:

Figure 4.6 – The psycopg2 PostgreSQL adapter
  1.  Now, open the settings file of the project to declare the database's RDS endpoint from AWS along with the username, password, and database name, as follows: 
(venv) [root@ip-172-31-95-213 atmproject]# vi atmproject/settings.py
------------------------------------------
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'atm',
'USER': 'dba',
'PASSWORD': 'bookdemo',
'HOST': 'atm.ck5074bwbilj.us-east-1.rds.amazonaws.com',
'PORT': '5432',
}
}
------------------------------------------
  1.  After the preceding command has been executed, the console should look similar to the following screenshot. Now you must save and close the file:

Figure 4.7 – The PostgreSQL settings inside Django
  1.  Next, let's create a database schema for this app with the help of the following command:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py migrate

Once the preceding command has been executed, the console should appear as follows:

Figure 4.8 – Creating the Django database schema
  1. Now open pgAdmin; you will see that 10 more tables have been added to the RDS, as shown in the following screenshot: 

Figure 4.9 – Django tables
  1. As shown in the following script, we need to create a Django superuser for the app:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py createsuperuser

Please fill in these values as follows:

  • Username = admin.
  • Email address = (this is your email).
  • The password requires eight characters; for example, here, we can type in djangoapp.

After the preceding command has been executed, the console should look similar to the following screenshot:

Figure 4.10 – The Django superuser

This user is not the PostgreSQL master user for the RDS. While the database username is used by your app to access the database, the Django superuser is used to log into the Django admin site. 

  1. Please set up the inbound rule to allow access to your Django site at port 8000 from any browsers, as shown in the following screenshot:

Figure 4.11 – The Django web security group
  1. As our EC2 instance's IP address is 3.209.184.46 and the public DNS for this EC2 instance is ec2-3-209-184-46.compute-1.amazonaws.com, add the following two lines of code into the Django project settings:
(venv) [root@ip-172-31-95-213 atmproject]# vi atmproject/settings.py
------------------------------------------
ALLOWED_HOSTS = ['3.209.184.46', 'ec2-3-209-184-46.compute-1.amazonaws.com']
AUTHENTICATION_BACKENDS = (
('django.contrib.auth.backends.ModelBackend'),
)
------------------------------------------

Once the preceding command has been executed, the console should look similar to the following screenshot:

Figure 4.12 – The Django project settings
  1. Now we can start our new Django project with the help of the following command:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py runserver 0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 10, 2019 - 06:29:24
Django version 2.2.2, using settings 'atmproject.settings'
Starting development server at http://0:8000/
Quit the server with CONTROL-C.
  1. Next, open the http://ec2-3-209-184-46.compute-1.amazonaws.com:8000/ link inside the browser. A successful message should appear in your browser, for instance, The install worked successfully! Congratulations!:

Figure 4.13 – Starting our Django project
  1. We can also view the Django admin site with the help of the http://ec2-3-209-184-46.compute-1.amazonaws.com:8000/admin link. Use this link to show the Django admin site, as follows:

Figure 4.14 – The Django admin web
  1.  Use the Django superuser to log in with the following credentials: Usernameadmin and Passworddjangoapp. This is shown in the following screenshot:

 

Figure 4.15 – Django administration

From the preceding general admin interface, we need a method to capture the application data inside the admin web. In Django, this can be created using database models.

Database models in Django

After signing in to the Django admin, we have not yet loaded our ATM location data from the PostgreSQL RDS into our Django web. Therefore, we have to create our database model by performing the following steps:

  1. The first step is to edit the models.py file; let's do it according to the following code block:
(venv) [root@ip-172-31-95-213 atmproject]# vi atmapp/models.py
------------------------------------------
from django.db import models
# Create your models here.
class ATMlocations(models.Model):
ID = models.AutoField(primary_key=True)
BankName = models.CharField(max_length=60)
Address = models.CharField(max_length=50)
County = models.CharField(max_length=15)
City = models.CharField(max_length=15)
State = models.CharField(max_length=2)
ZipCode = models.IntegerField()
class Meta:
db_table = 'ATM locations'
verbose_name = 'ATM location'
verbose_name_plural = 'ATM locations'
------------------------------------------

After the preceding command has been executed, our console will appear as follows:

Figure 4.16 – The database model
  1. The next step is to check for the app name inside the apps.py file with the help of the following command:
(venv) [root@ip-172-31-95-213 atmproject]# cat atmapp/apps.py
------------------------------------------
from django.apps import AppConfig

class AtmappConfig(AppConfig):
name = 'atmapp'
------------------------------------------
  1. In order to include the app within our project, we need to add a reference to its configuration class in the settings. This is atmapp.apps.AtmappConfig, as shown in the following screenshot:

Figure 4.17 – Including an app inside the Django project

Of course, from the preceding database model, Django needs to be able to access the correct underlying table in the PostgreSQL RDS. In order to link the database model to the underlying table, you can try to migrate your database. We will demonstrate this in the next section.

Migrating the database

We can now update the new ATM locations model inside the Django app, as follows:

  1. We will write the following makemigrations command, which is used to create the ATM locations model:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py makemigrations atmapp
Migrations for 'atmapp':
atmapp/migrations/0001_initial.py
- Create model ATMlocations
  1. We will generate the SQL of ATMlocations using the sqlmigrate command:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py sqlmigrate atmapp 0001
BEGIN;
--
-- Create model ATMlocations
--
CREATE TABLE "ATM locations" ("ID" serial NOT NULL PRIMARY KEY, "BankName" varchar(60) NOT NULL, "Address" varchar(50) NOT NULL, "County" varchar(15) NOT NULL, "City" varchar(15) NOT NULL, "State" varchar(2) NOT NULL, "ZipCode" integer NOT NULL);
COMMIT;
  1. We will apply the migration to the PostgreSQL RDS, as shown in the following code block. However, since the ATM locations table already exists, the migrations will not be applied:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py migrate
Operations to perform:
Apply all migrations: admin, atmapp, auth, contenttypes, sessions
Running migrations:
No migrations to apply.

If the RDS does not contain the ATM locations table already, then this table will be created and the output will be as follows:

 Running migrations:
Applying atmapp.0001_initial... OK

The database model is now ready to be registered inside the ATM app. We will do this in the next section. After that, we can try to make changes to our ATM locations' data using Django web.

Understanding the Django user interface  admin, views, templates, and URLs

A view inside a Django application generally performs a specific function and renders the results to a specific template. So, each view is a simple Python function or class method. Django will select a suitable view by examining the requested URL. Django URL patterns are very elegant; they are the general form of a URL. In order to select a view from the URL, Django uses a set of patterns called URLconfs. A URLconf will map URL patterns to their corresponding views.

Making the atmapp modifiable inside the admin

We need to tell the admin that the ATMlocations objects have an admin interface:

  1. To do this, open the atmapp/admin.py file and edit it to look like the following code block:
[root@ip-172-31-95-213 atmproject]# vi atmapp/admin.py
------------------------------------------
from django.contrib import admin
# Register your models here.
from .models import ATMlocations
admin.site.register(ATMlocations)
------------------------------------------
  1. Then, we can launch the Django server with the help of the following command:
(venv) [root@ip-172-31-95-213 atmproject]# python3.7 manage.py runserver 0:8000
  1. After that, we will log in as the admin at http://ec2-3-209-184-46.compute-1.amazonaws.com:8000/admin. Using the admin and djangoapp login credentials, we will be able to view our captured ATM locations data from the PostgreSQL RDS, as shown in the following screenshot:

Figure 4.18 – Registering the database model inside a Django app

In the next section, we will discover which operations are supported by Django in the ATM locations file.

Exploring the free admin functionality

The Django administration provides support for us to perform viewing, updating, adding new, and deleting ATM location records. To carry out these tasks, perform the following steps:

  1. Click onto the ATM locations link; it will open 654 data objects, as shown in the following screenshot:

Figure 4.19 – Using the admin interface
  1. Next, click on any of the data objects. Then, the details of its ATM will appear, as shown in the following screenshot:

Figure 4.20 – Viewing an ATM location
  1. Now we will modify BankName to Citibank NA and then click on the SAVE button. We will get a confirmation message after we have saved the new bank name, as shown in the following screenshot:

Figure 4.21 – Changing an ATM location
  1. In the preceding screen of ATMapp (Figure 4.20), if you click on the HISTORY button, you can view the history of all the actions that have happened to that ATM, as shown in the following screenshot:
Figure 4.22 – Data object history

So far, you have performed all of the steps in our Django project of ATMs. Moving forward, you can apply the same steps to create your own Django projects.

Summary

In this chapter, we demonstrated a step-by-step process of how to implement the ATM banking PostgreSQL RDS inside a Django app. You learned how to set up Django within a virtual environment. We used the postgresql_psycopg2 engine for Django to connect to our PostgreSQL RDS, and then we learned how to add a new database model. We activated the database model and managed it by using the built-in admin tool. Finally, we also learned how to create views, templates, and URLs

In the next chapter, we will learn how to create geospatial functions using PostGIS for our PostgreSQL ATM database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.229.253