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: Show
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:
The following figure illustrates the Django MTV architecture and how the Model, Template and View components interact each other: Image by Author2 Install DjangoFirstly, 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 django-admin startproject mywebsite 2. This article explains how to build a Python django-admin startproject mywebsite 2.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 mywebsite 4 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 mywebsite 4 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 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. Image by AuthorThe folder mywebsites contains two elements:
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 mywebsite 7 script, as shown:DATABASES = { You can run the development Web server from within this folder using manage.py and the django-admin startproject mywebsite 8 command:cd mywebsite 4 Start a New AppWithin 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 django-admin startproject mywebsite 9 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 = { 0 script.INSTALLED_APPS = [ Now you must say the Web server that a new app has been added. You can do this through the DATABASES = { 1 command. You can launch it from the root directory of the project:python3 manage.py migrate 5 Create your DatabaseNow 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. Image by Authorfrom django.db import models 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 = { 2 command, followed by the DATABASES = { 1 command, which applies all the migrations not yet applied:python3 manage.py makemigrations myapp 6 Build the Data Entry Web InterfaceIn 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 mywebsite 0Now, you must grant the admin access to all your model classes. This can be done by editing the DATABASES = { 4 script. You should add a register option for each table you want grant access to the admin:django-admin startproject mywebsite 1The admin interface will be available at the following address: http://127.0.0.1:8000/admin/. It will look like the following one: Image by AuthorThat’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.
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 GradioA 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 StreamlitA 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 EmphasisGetting 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 :) Can you use Python for data entry?You can easily use Python to automate data entry and improve your productivity.
How do I create a simple data entry application?To add a new record to your table using the data entry form, perform these steps:. Select any cell in your table.. Click on the Form button on the Quick Access Toolbar or on the ribbon.. In the input form, click the New button.. Type the information in the appropriate fields.. How do I automate data entry in Excel with Python?To work on this Excel sheet we are going to use a library openpyxl. Create a folder in your directory, give it a name and install the openpyxl package by executing the following command in your terminal. Now we can import this package to work on our spreadsheet. Before that add the spreadsheet in your project folder.
|