Building a Fast Web Interface in Django for Data Entry | by Angelica Lo Duca | Towards Data Science

In this article I describe a simple strategy to build a fast Web Interface for data entry in Django. The article covers the following topics:
Overview of Django
Install Django
Create a new Project
Create a New App
Create your Database
Build the Data Entry Web Interface
Django is a Python Web framework designed to build fast Web Applications. A single instance of Django is called Project. A Project may contain one or more Apps.
Django follows the Model-Template-View (MTV) architecture. MTV differs from the Model-View-Controller (MVC) architecture in the sense that the Controller part is already implemented by the framework itself through templates.
The Django MTV architecture is composed of the following components:
Model: it defines the logical data structure. In practice, a model is a Python class, which represents a single table in the database. All the classes representing the logical structure of the database are stored in a script named models.py.
View: it defines the business logic, in the sense that it communicates with the model and translates it into a format readable by the Template. A view is a Python function, which takes a request as input and returns a Web response as output. All the functions representing the business logic are stored in a script named views.py.
Template: it defines the structure or the layout of a file, such as a HTML file. It is a text document or a Python string encoded through the Django template language. All the templates are stored in a subdirectory of the App named templates.
The following figure illustrates the Django MTV architecture and how the Model, Template and View components interact each other:
Firstly, the Django library must be installed, through the following command:
pip3 install django
You may decide to install it either directly in your main working environment or into a specific virtualenv. This article explains how to build a Python virtualenv.
Before creating a new project, you must configure the associated database. Thus, you must have a running relational database on your machine (e.g. MySQL or Postgres). Personally, I have installed the MySQL server. Since I had an old installation of XAMPP, I have exploited the MySQL server provided by it. In PhPMyAdmin, I have created a database called mywebsite_db, initially empty. Django will populate it.
In order to communicate with the SQL server, Django needs a Python driver for SQL. In the case of MySQL, you should install the mysqlclient Python library.
Depending on you Operating System, you may encounter some installation issues, especially if your OS is quite old. Personally, I had some problems with the installation of the mysqlclient library, which I overcome by following the suggestions proposed at this link.
In the next sections, I give an overview of how to build and run a Django app. For more details, you can refer to the Django official documentation.
A single Django Web site instance is called Project. Within a Project, you can run many Web Apps. You can create a new Project using the django-admin tool:
django-admin startproject mywebsite
where mywebsite is the name of your Project. This command will create a new directory called mywebsite, in the folder from which you run the previous command.
The folder mywebsites contains two elements:
manage.py, which is the main script for configuration
a folder with the same name of your project (inner mywebsite).
The figure on the left shows the content of the inner mywebsite folder.
Before running the new Project, you must configure the database parameters in the inner mywebsite/settings.py script, as shown:
DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'NAME': 'mywebsite_db',        'USER': 'root',        'PASSWORD' : 'YOUR PASSWORD'    }}
You can run the development Web server from within this folder using manage.py and the runserver command:
cd mywebsitepython3 manage.py runserver
Within a Project you can run as apps as you want. In order to create an app you can enter the project folder and run the following command:
python3 manage.py startapp myapp
where myapp is the App name.
Note that once a project is created, all the operations on it will be done through the manage.py script, such as startapp, migrate (as you will read later in this article, and so on).
If you want to activate the new App, you must add it to the list of active Apps, contained in the inner mywebsite/setting.py script.
INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    # Add you new application,    'myapp.apps.MyappConfig']
Now you must say the Web server that a new app has been added. You can do this through the migrate command. You can launch it from the root directory of the project:
python3 manage.py migrate
Now the environment is ready. You can concentrate on content, in terms of data model and Web site presentation.
The model can be modified in this file:
myapp/models.py
Each table in the database corresponds to a class in the model. For example, the table can be easily translated into the model below.
from django.db import modelsclass Person(models.Model):    Name = models.CharField(max_length=64)    Surname = models.CharField(max_length=64)    BirthDate = models.DateTimeField()    Sex = models.CharField(max_length=1)
The model is independent on the specific SQL database, thus can be translated by Django into a whatever SQL database (MySQL, Postgres...)
Once created the model, you must tell Django that you have made some changes to your model. This can be done through the makemigrations command, followed by the migrate command, which applies all the migrations not yet applied:
python3 manage.py makemigrations myapppython3 manage.py migrate
In order to create the Web Interface, you can exploit a Django functionality, which is based on the admin interface. You can activate the admin interface through the following command:
python3 manage.py createsuperuser
Now, you must grant the admin access to all your model classes. This can be done by editing the myapp/admin.py script. You should add a register option for each table you want grant access to the admin:
from django.contrib import adminfrom .models import Personadmin.site.register(Person)
The admin interface will be available at the following address: http://127.0.0.1:8000/admin/. It will look like the following one:
That’s all folks!
In this article I have described how to build a fast Web Interface for data entry in Django. After an initial setup of the environment, deploying the Web interface is very simple, because it can exploit the admin interface provided by Django.
And you, which framework do you exploit to build fast Web Interfaces? Drop me a comment! I will be glad to listen to your opinion!
If you have come this far to read, for me it is already a lot for today. Thanks! You can read more about me in this article.
If you want to read more about how Django works, you can read this interesting article by Rinu Gour, who explains the Django MTV Architecture.
pub.towardsai.net
towardsdatascience.com
towardsdatascience.com
One more word before leaving: you can download the full code of this tutorial from my Github repository :)