Django Framework | Introduction and walkthrough

Django-logo

Introduction

Django is a python based free and open-source web framework which follows the MTV (model-template-view) architectural pattern. Developed in late 2003, Its primary goal is to ease the creation of complex, database-driven websites. The framework emphasizes reusability and “pluggability” of components, less code, low coupling, rapid development, and the famous principle of programming ‘be DRY’ (don’t repeat yourself).

Some well-known sites that use Django include Instagram, Mozilla, Disqus, Bitbucket, and Nextdoor.

Also included in the core framework are:

  • a lightweight and standalone web server for development and testing
  • a form serialization and validation system that can translate between HTML forms and values suitable for storage in the database
  • a template system that utilizes the concept of inheritance borrowed from object-oriented programming
  • a caching framework that can use any of several cache methods
  • support for middleware classes that can intervene at various stages of request processing and carry out custom functions
  • an internal dispatcher system that allows components of an application to communicate events to each other via pre-defined signals
  • an internationalization system, including translations of Django’s own components into a variety of languages
  • a serialization system that can produce and read XML and/or JSON representations of Django model instances
  • a system for extending the capabilities of the template engine
  • an interface to Python’s built-in unit test framework

The main Django distribution also bundles a number of applications in its “contrib” package, including:

  • an extensible authentication system
  • the dynamic administrative interface
  • tools for generating RSS (Really Simple Syndication) and Atom syndication feeds
  • a “Sites” framework that allows one Django installation to run multiple websites, each with their own content and applications
  • tools for generating Google Sitemaps
  • built-in mitigation for cross-site scripting, SQL injection, password cracking and other typical web attacks, most of them turned on by default
  • a framework for creating GIS applications

So overall it is a versatile framework supporting many famous databases backends such as MySQL, SQLite, PostgreSQL, Oracle, etc., giving a programmer full freedom to design his web app as he wants, with built-in security from many web attacks.


Pros and cons of Django framework:


Pros:

Fast development

Django’s many built-in functions and classes make it easy for the job to get done.

Security

Django has built-in mitigation for many web attacks which adds security to your website.

Written in one of the easiest languages

Nonetheless, it is written in one of the easiest languages and the first choice for many coders around the globe – Python.

Cons:

Maybe it is not the best choice in some cases .

Django was developed to lower the load of maintaining big websites, so if you are making a static one-pager or any microservices, Django might not be the ideal framework for it.

Speed:

As Django is a python-based framework, which is an interpreted language it could it could have been a slow framework for big websites where there is a lot of server-client interaction. But Django solves this with its caching optimizing abilities. So, speed itself is not a problem, but you have to ensure a good and scalable architecture.

Lack of convention:

Unlike Ruby on Rails or other frameworks which have a Convention Over Configuration approach, in Django, you have to define it all yourself.


Walkthrough

Django can be installed by doing a simple pip installation from a terminal which is

> pip install django

You can confirm the installation by typing in the terminal

> django-admin

This will give you a list of subcommands. You can study them by typing

> django-admin help [subcommand]

Just to get a glimpse of the Django framework, we will make a test project and study the files it generated for us. To start a new project in Django

> django-admin startproject testproject

You can give whatever name you want to it instead of the test project.

This will generate a folder by the name you gave it in the terminal. In my case, it is testproject. Inside this folder, there is another folder with the same name and a file called manage.py. This folder is the main project directory of your project. The manage.py file is Django’s command-line utility for administrative tasks. We will be using this file later on in our series.

Let’s now check the other folder is generated. There are 5 files generated by default in this folder.

1) __init__.py

The __init__.py is an empty file that just informs python that this is a python package.

2) asgi.py

It is the default config file generated by Django for ASGI deployment. ASGI (Asynchronous Server Gateway Interface) is a spiritual successor to WSGI, intended to provide a standard interface between async-capable Python web servers, frameworks, and applications. We will not be touching this file in our series.

3) settings.py

It is the main settings configuration file for a Django project. We will be making some changes to this file from time to time in our series.

4) urls.py

The urls.py file is the main URL configuration file for a Django project. It routes all the URLs of our Django project. Although we will be using a separate ursl.py file for each of our apps and then connects it to this main urls.py file. If you don’t know what is an App in Django then we will be taking it up shortly.

5) wsgi.py

Wsgi.py file contains the default configuration for WSGI (Web Server Gateway Interface). Django’s primary deployment platform is WSGI, the Python standard for web servers and applications.


Project vs App in Django

A project in Django is the main folder that is generated when we do the ‘django-admin startproject [projectname] command in the terminal. It contains all files concerning our website. Whereas an App is just a part of your website. To make it more clear let’s take an example.

Suppose we are making a blog website. In it, we may have an app called ‘blog’ which will have all the code concerning our blog posts. Then we might have another app in our project called ‘users’ which will have all our code concerning our user authentication etc. So, a Django project is a big folder containing many Django apps. Let’s see this in practice.

To create an app in our project, fire up your terminal and change your directory into the main folder we created with the command ” django-admin startproject testproject

 > cd testproject

Then to create an app the command is

> django-admin startapp app

You can give any name in place of “app”. This will create a new folder in our main testproject directory called “app” or whatever name it. By default, Django generates 6 files and a folder called migrations in it. Let’s go through these files one by one.

1) migrations (folder)

This folder will have a record of the migrations we will make to update our database. We will see migration and databases later on in our series. It just has an empty __init__.py file which just says that this is a python package.

2)__init__.py

Again, as we know this file just lets the python interpreter know that this directory contains code for a python module.

3)admin.py

This file is used to register our models in the default Django admin page it creates. We will see more about models and the Django admin page later in our series.

4) apps.py

This file contains just a class which represents a Django application and its configuration.

5) models.py

The model.py is the file in which we will be creating models for our app to store it in a database. We will be studying about models later on in our series where we will actually see it in practice.

6) tests.py

It is a file that helps you to run tests for your Django application. We will not be using this file in our series.

7) views.py

Django’s views are the information brokers of a Django application. A view sources data from your database (or an external data source or service) and delivers it to a template. In other words, it decides what data will be seen on your website. Now that we have quite a good knowledge about the files in our project, lets see about the MVT structure of Django framework.


MVT architecture of Django

MVT stands for Model-View-Template.

1)Models

 A model provides the interface for the data stored in the Database. It is responsible for maintaining the data and handling the logical data structure for the entire web application.

2)Templates

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. 

3)Views

In Django, Views act as a link between the data of a model and the Templates. They are responsible for handling all the business logic behind the web app. It acts as a link between Models and Templates.

1) A user sends a request to Django

2) Django then searches for the URL

3) If a URL path links up to a View, then that particular View is called.

4) The view will then interact with the model and retrieve the appropriate data from it.

5) The view then renders back the retrieved data to the user with the appropriate template.


That’s all for this post about the Django framework and its intro. In our next part of our Django series we will begin developing a full fledged Django web blog. It will have the functionality of creating blog posts, updating them, delete them. We will also add user-authentication so we can have more users on our website to write more blogs. To learn more about Django framework in more detail, check out their documentation.

Stay tuned and keep learning !!


Share:

Author: Ayush Purawr