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:
Image by Author2 Install Django
Firstly, the Django library must be installed, through the following command:
pip3 install djangoYou may decide to install it either directly in your main working environment or into a specific django-admin startproject mywebsite2. This article explains how to build a Python django-admin startproject mywebsite2.
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 django-admin startproject mywebsite4 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 django-admin startproject mywebsite4 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.
3 Start a New ProjectA 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 mywebsitewhere 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.
Image by Author
The folder mywebsites contains two elements:
- django-admin startproject mywebsite6, 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 django-admin startproject mywebsite7 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 django-admin startproject mywebsite8 command:
cd mywebsitepython3 manage.py runserver4 Start a New App
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 myappwhere myapp is the App name.
Note that once a project is created, all the operations on it will be done through the django-admin startproject mywebsite9 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 DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mywebsite_db',
'USER': 'root',
'PASSWORD' : 'YOUR PASSWORD'
}
}0 script.
'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 DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mywebsite_db',
'USER': 'root',
'PASSWORD' : 'YOUR PASSWORD'
}
}1 command. You can launch it from the root directory of the project:
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.pyEach table in the database corresponds to a class in the model. For example, the table can be easily translated into the model below.
Image by Authorfrom django.db import models
class 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 DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mywebsite_db',
'USER': 'root',
'PASSWORD' : 'YOUR PASSWORD'
}
}2 command, followed by the DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mywebsite_db',
'USER': 'root',
'PASSWORD' : 'YOUR PASSWORD'
}
}1 command, which applies all the migrations not yet applied:
python3 manage.py migrate6 Build the Data Entry Web Interface
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:
django-admin startproject mywebsite0Now, you must grant the admin access to all your model classes. This can be done by editing the DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mywebsite_db',
'USER': 'root',
'PASSWORD' : 'YOUR PASSWORD'
}
}4 script. You should add a register option for each table you want grant access to the admin:
The admin interface will be available at the following address: //127.0.0.1:8000/admin/. It will look like the following one:
Image by Author
That’s all folks!
SummaryIn 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.
Related ArticlesBuilding a Fast Interactive Dashboard in Jupyter through Gradio
A ready-to-run tutorial on Gradio, a very powerful Python package for Machine Learning demos.
pub.towardsai.net
How to Run Animations in Altair and Streamlit
A ready-to-run tutorial, which describes how to build an animated line chart using Altair and Streamlit.
towardsdatascience.com
Data Visualisation Principles Part 2: Layout and Emphasis
Getting started with basic Graphic Design principles.
towardsdatascience.com
One more word before leaving: you can download the full code of this tutorial from my Github repository :)