Table of contents
1.
Introduction
2.
Role of Django
3.
The MVT Architecture of Django
4.
Working of Django
5.
Installation of Django
5.1.
Creating a Project in Django
6.
Advantages of Shifting to Django
7.
Frequently Asked Questions
7.1.
What is Django?
7.2.
What are some companies that use Django?
7.3.
What are the characteristics of Django?
7.4.
How do we determine the version of Django installed on our system?
7.5.
What is the MVT architecture in Django?
8.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Basics of Django Framework

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The goal of a framework is to free designers and developers to focus on creating unique features for their web-based projects rather than re-inventing everything through coding. 

A framework is specifically designed to assist us in improving the performance and efficiency of our web app development task. Django is an open-source Python-based web framework that is free for use and employs the Model-view-template design pattern.

OG Image

In this article, we will at a basic overview of what Django is, how it works and how it helps developers increase their productivity during development.

Role of Django

To all those who have been in the web development domain for quite some time, they must be thinking, why Django? We've already made websites, which are hosted and working fine for years now. 

So, the answer is frameworks exist to save developers time. It's the developer's choice to do everything from scratch or use a framework for better performance. Similarly, Django is a framework that offers an extensive collection of modules to use in our projects. It makes handling the backend development extremely easy and handy, which was quite a complex task when using PHP and other things.

Django was developed by a web team in charge of developing and maintaining newspaper websites. That team discovered repetitive code and design patterns, paving the way for creating a generic web development framework, open-sourced in July 2005 as the "Django" project.

Not only this, Django has a lot to offer. Let's see its MVT architecture and its difference from the long-existing MVC architecture.

The MVT Architecture of Django

Django employs a different architecture known as MVT (Model View Template). Before we dig deeper into this, let's understand its alternative MVC.

MVC (Model View Controller) architecture has been used in the software industry for a long time. MVC is used in almost every language, with slight variations, but the concept remains the same. In simpler terms, we can explain them as follows:

  • Model: A model serves as an interface for data stored in a database. It is in charge of data maintenance and handling the logical data structure for the entire web application.

 

  • Views: In MVC, a view is a user interface. It is in charge of displaying Model Data to the user and gathering information from the user. Views in MVC are not the same as Django Views. We will discover the distinction later in this article.

 

  • Controller:  In MVC, a controller is in charge of the entire web application logic. The controller sees the request when a user uses a view, sends an HTTP request, and responds appropriately.

 

Refer to the MVC diagram to understand it completely.

MVC architecture

Django implements its logic in its web app, so its framework handles all controller components. As a result, Django employs a distinct architecture called MVT (Model View Template). Now let us look at the MVT architecture.

  • Model in Django: Models are Python objects that define the structure of an application's data and provide mechanisms for managing (adding, modifying, deleting) and querying database records.
     
  • Views in Django: Views in Django serve as a bridge between Model data and Templates. Views in Django MVT, like controllers in MVC, handle all business logic for the web app. It serves as a link between Models and Templates. It receives the user request, retrieves relevant data from the database, and renders the template along with the retrieved data.
     
  • Templates in Django: Django uses templates like View in MVC. Templates are entirely in charge of the User Interface. It handles all of the web page's static elements, and the HTML visitors will see.

 

Refer to the diagram below to understand the MVT architecture in detail.

MVT architecture

As a result, there is no separate controller in the Django MVT architecture, and everything is based on Model View Template, hence the name MVT.

Let’s see the working of the Django framework.

Working of Django

A web application in a traditional data-driven website waits for HTTP requests from the web browser (or other clients). When a request is received, the application determines what is required based on the URL and possibly data in POST or GET methods. Depending on what is needed, it may read or write data from a database or perform other tasks to fulfill the request. 

The application will then respond to the web browser by inserting the retrieved data into placeholders in an HTML template and dynamically creating an HTML page for the browser to display. Django web applications usually separate the code for each step into separate files. The diagram below explains the Django Framework methodology.

working of django

As seen in the above diagram, the steps involved in the working are:

  1. Django receives a URL request for a resource from the user.
  2. The Django framework then looks for the URL resource.
  3. If the URL path points to a View, that View is invoked.
  4. Now the View will interact with the Model to retrieve the necessary data from the database.
  5. The View then returns the user an appropriate template and the retrieved data.

Now, let's start with installing Django and setting up a Django Project.

Installation of Django

First, we will install and set up Django. After that, we will see how we can build a basic web application using Django. We'll need Python installed and knowledge of virtual environments and Python's package manager, pip, to finish the setup.

Installation using pip: Django can be installed easily using pip. PIP is a package manager for Python that uses the Python Package Index to install Python packages.

Step 1: To download and install Django.

In the command prompt, execute the following command: 

pip install Django
You can also try this code with Online Python Compiler
Run Code

 

Step 2: Verify Django installation

In the command prompt, execute the following command:

python -m django --version
You can also try this code with Online Python Compiler
Run Code

 

Now, let’s see how to create a project in Django

Creating a Project in Django

We need to do some preliminary work while using Django for the first time. We'll need to auto-generate some code that creates a Django project, a collection of settings for a Django instance, such as database configuration, Django-specific options, and application-specific settings.

Step 1: Cd into the directory where you want to save your code from the command line, then run the following command:

$ django-admin startproject django_app
You can also try this code with Online Python Compiler
Run Code

 

This will create a django_app directory in our current directory. The name of our project will be django_app.

Let's take a look at what startproject command generated:

directory image

Step 2: Running the development server to check if our Django project is up and running.

$ python manage.py runserver
You can also try this code with Online Python Compiler
Run Code
screen shot

As you can see that the Django project is running, let’s create apps for our project.

Step 3: To get started, make sure that you are in the django_app directory, now run the following command:

$ python manage.py startapp polls
You can also try this code with Online Python Compiler
Run Code

This will generate a polls directory, now our directory looks like this:

directory image

The poll application will be stored in this directory structure. And now you can start developing your projects by writing views and models.

Must Read PHP Projects With Source Code

Advantages of Shifting to Django

Django offers a variety of advantages to developers, some of which are mentioned below,

Speedy Development

Django allows us to create fully functional web applications in less time. Because everything we need for the development is part of the same "product", it all works seamlessly together. It adheres to consistent design principles, which aids us in the rapid development process.

Versatile Framework

Django can be used to create nearly any type of website. There is something for everyone, from content management systems and wikis to social networks and news sites. It is compatible with any client-side framework and can deliver content in nearly any format (HTML, RSS feeds, JSON, XML, etc.).

Internally, while it offers options for almost any functionality you might require (e.g., several popular databases, templating engines, and so on), it can also be extended to use other components if necessary.

Security

Django, by default, protects against a wide range of vulnerabilities, including SQL injection, cross-site scripting, cross-site request forgery, and clickjacking.

Django provides a secure way to manage user accounts and passwords, and it avoids common mistakes such as storing session information in cookies (instead, cookies only contain a key, and the actual data is stored in the database).

Scalability

Django employs a component-based architecture, meaning each component is independent of the others and can thus be replaced or changed as needed. Because there is a clear separation between the various parts, it can be scaled by adding the hardware at any level: caching servers, database servers, or application servers. 

Maintainability

Django code is written under design principles and patterns that promote the creation of maintainable and reusable code. It employs the Don't Repeat Yourself (DRY) principle to avoid unnecessary duplication, thereby reducing the amount of code.

Portability

Django is written in Python and it runs on a variety of platforms. This means we can run our applications on Linux, Windows, and Mac OS. Furthermore, Django is well-supported by many web-hosting providers, who frequently provide specialized infrastructure and documentation for hosting Django sites.

Frequently Asked Questions

What is Django?

Django is an open-source Python-based web framework which is available free for use, and it employs the Model-view-template design pattern.

What are some companies that use Django?

Instagram, DISCUS, Mozilla Firefox, YouTube, Pinterest, Reddit, and other companies use Django.

What are the characteristics of Django?

Some prominent characteristics of Django include a fully functional framework with authentication, content administration, and RSS feeds, highly secure, highly scalable and adaptable.

How do we determine the version of Django installed on our system?

To find out what version of Django is installed on the system, open a command prompt and type the following command: python -m django --version

What is the MVT architecture in Django?

MVT stands for Model View Template, a software design pattern used by Django.

Key Takeaways

In this article, we have covered the basics of the Django framework. We looked at its benefits and saw how it fits into the web development process. In the end, we saw how we could install Django and create our first project in Django.

If you liked this blog and want to read some blogs related to this topic, then here are some detailed blogs, Django TemplatesDjango Forms, and CRUD operations in Django.

We hope this article has helped you in some way, and if you liked our article, do upvote our article and help other ninjas grow. You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Happy Learning!

Live masterclass