Python 3x Pandas Django

Django Tutorial


Django is a high-level Python web framework that assists in building and maintaining modern web applications. Django makes it easier to build better web apps quickly and with less code. Our tutorial gives a complete understanding of Django.

Prerequisites

Before you proceed, make sure that you understand the basics of python programming procedural and OOP (Object Oriented Programming): control structures, data structures and variables, classes, objects, packages, functions, methods etc.

Audience

Our tutorial is designed for developers who want to learn from the scratch (beginner to expert level) and how to develop quality modern web apps using the python libraries and tools offered by Django.

Getting started with Django

Before you get started, let’s check out the topic MVC Architecture, Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications.

A Model View Controller pattern is made up of the following three parts −

• Model: It includes all the data and its related logic

• View: Present data to the user or handles user interaction

• Controller: An interface between Model and View components

In other words,

• Model: Database used to store a data and its related logic

• View: HTML page used to present data to the user or handles user interaction

• Controller: Controller is used to control the user\model request and response

MVC Diagram

MVC Architecture

MVC architecture has been there for a long time in the software industry. All most all languages employ MVC with slight variation, but the concept remains the same.

Django prefers to use its own logic implementation in its web app and hence its framework handles all the controller parts by itself. Hence Django implements a different kind of architecture called MVT (Model – View – Template) architecture.

A Model View Template pattern is made up of the following three parts −

• Model: Just like the Model in MVC, here as well it has the same functionality of providing the interface for the data stored in the database.

• View: Just like the controller in MVC, views in Django MVT are responsible for handling all the business logic behind the web app. It acts as a bridge between Models and Templates.

It sees the user request, retrieves appropriate data from the database, then renders back the template along with retrieved data.

• Template: Just like View in MVC, Django uses templates in its framework. Templates are responsible for the entire User Interface completely. It handles all the static parts of the webpage along with the HTML, which the users visiting the webpage will perceive.

MVT Diagram

All the codes of the Django framework are written in Python, which runs on many platforms. Which leads to run Django too in many platforms such as Linux, Windows and Mac OS.

Installation of Django

Step 1 – Installing Python

Install python3 if not installed in your system. you can download and install the latest version of Python from the link.

IMPORTANT:

• pip is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers.

• A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide.

• venv is the standard tool for creating virtual environments, and has been part of Python since Python 3.3. Starting with Python 3.4, it defaults to installing pip into all created virtual environments.

• virtualenv is a third party alternative (and predecessor) to venv. It allows virtual environments to be used on versions of Python prior to 3.4, which either don’t provide venv at all, or aren’t able to automatically install pip into created environments.

Step 2 – Set up a virtual environment

A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide.

To create a virtual environment (with the name of “tutorial1-env”. If you want, you can also use some other name virtual environment name), decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

python –m venv tutorial1-env

Command Prompt Create Virtual Env

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.

Virtual Env Directory

Once you’ve created a virtual environment, you may activate it.

tutorial1-env\Scripts\activate.bat

pip list will display all of the packages installed in the virtual environment:

PIP List

Our newly virtual environment “tutorial1-env” has pip, setuptools packages installed so we need to install Django in the next step3

For Unix or MacOS user, refer this link

Step 3 – Installing Django

Installing Django is very easy, but the steps required for its installation depends on your operating system. Since Python is a platform-independent language, Django has one package that works everywhere regardless of your operating system.

You can download the latest official version is 3.2.6 (LTS - long-term support releases) from the link

On Window,

pip install Django==3.2.6

Command Promompt Install Django

Again run the command - pip check (checking installed packages)

Command Promompt PIP List

You can see Django and some other packages added newly.

Note: deactivate the virtual environment

On Window, deactivate

Command Promompt Deactivate Virtual Env

To activate again,

On window, tutorial1-env\Scripts\activate.bat

Command Promompt Virtual Env Activate


Step 4 – Pre-Checks

Once you install Python and Django successfully. You can verify it by typing python command at a command prompt (Window OS). If you see something like this, then Python is installed.

On window, python --version

Command Promompt Python Version

On window, django-admin --version

Command Promompt Django Version

Congratulations! We just finished the system setup and let's move to the next session Getting Started with Django

If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!