• Articles
  • Tutorials
  • Interview Questions

Django Tutorial - Learn Django from Scratch

Table of content

Show More

Introduction to Django

Most of the web applications, apart from their core features, consist of common functionalities such as authentication, database connectivity, etc. Having predefined APIs or classes to solve specific web development problems makes Web Developers’ job a lot easier.

These predefined APIs and tools that can be used to simplify common web development operations are known as the web framework. One of the most popular Python web frameworks is Django.

Introduction to Django Web Framework

First of all, watch this video on ‘Django for Beginners’:

Video Thumbnail
Youtube subscribe

Without further ado, let’s get started.

What is Django Web Framework?

Django is a web framework written in Python. A web framework is a software that supports the development of dynamic websites, applications, and services. It provides a set of tools and functionalities that solves many common problems associated with web development, such as security features, database access, sessions, template processing, URL routing, internationalization, localization, and much more.

what is django web framework

Why Django Web Framework?

First of all, it is a Python web framework, which means that you can benefit from a wide range of open-source libraries out there. This popular web framework also offers a standalone web server for development and testing, caching, and middleware system. Moreover, it provides the ORM (object-relational mapper) library, the template engine, form processing, and an interface with Python’s unit testing tools.

Certification in Full Stack Web Development

How to Install Django

Prerequisites for Installing Django

Check if Python is installed in the system by using the following command:

python --version

If Python is not installed in your system, go through this tutorial on ‘How to Download and Install Python?’Installation process of Django

Install Django

Step 1: Open the command prompt
Step 2: Run the pip command

pip install django

Django installation

Architecture of Django Web Framework

Architecture of Django

Django web framework follows the MVT (Model View Template) architecture. This architectural pattern is designed for easy and rapid web development.

Architecture of Django Web Framework

The model or the model class is the source of information regarding the data. In other words, the model can be considered as the logical data structure behind the entire web application. Each model is associated with a table in the database.

The view or the view function takes up different web requests and returns respective responses. For instance, the request can be POST or GET type, and the response can be an HTML page, a 404 error, a redirect, or an image.

Template layer determines how a user sees the response Django returns. It basically controls the user interaction.
Let us talk about how these components communicate with each other to make the development in Django easy and rapid.

Get 100% Hike!

Master Most in Demand Skills Now!

Watch this video on ‘Python Django Tutorial’:

Video Thumbnail

Django web framework components

First, a user requests for a resource. Django considers the request as a URL and matches it with the existing URL paths in the urls.py file. This process of matching the user-requested URL to the one in urls.py is known as URL mapping. Once the URL matches, Django carries out the further process.

Once the URL is mapped, Django jumps to the views.py folder and calls a view. The triggered view looks for models and templates to interact with, and then it returns the response back to the user. Here, the model deals with the data associated with the user request. On the other hand, the template deals with the HTML and the static files, such as CSS files, JS files, images, etc., that are required to complete the view.

Alright, now that we know how the architecture works, let us move ahead with our Django tutorial and see how to use Django framework to build a web application.

Hands-on: Step-by-step Guide to Building a Web Application Using Django

We are going to build a simple web application that shows a simple message. First, we will create a Django project and inside that directory, we will create an application, where other operations will be performed. Let us go ahead and create a project first.
Building web application using Django Web framework

Creating a Project

In order to create a new Django project, run the following django-admin startproject command in the command prompt.
Creating a Project 1
Now, go to the folder where the command prompt was being run from. There you can find a new project directory that looks like this.
Creating a Project 2
Inside the ‘website’ directory (root directory), you will see another directory with name same as the root directory.
Creating a Project 3
Inside that directory the following default python files are created.Creating a Project 4
Using these files, you will be configuring the entire project. One project can contain different applications for different features, for instance, entire home page can be an application, user profile can be a separate application, etc. Without much delay, let us see how to create an application.

Creating an Application

  • Open the command prompt
  • Navigate to the directory where you have created the project

Creating an Application 4

  • Run the following command to create an app inside the project directory
    python manage.py startapp tutorial

    Creating an Application 3

Here, you will see what difference this command makes inside the project directory. There is a new folder called ‘tutorial’ inside the ‘website’ directory.
Creating an Application 2

Now, if you go inside the ‘tutorial’ application folder, you will see the following Python files Django created for you to configure the application.

Creating an Application 1

Alright, so far in this Django tutorial, we have learned how to create an application called ‘tutorial.’ Let us now see how to build our very first web application using Django.

Creating a URL and a View for the Web Application

Step 1: First of all, paste the URL file inside the newly created application. You can copy this file from the website directory

Step 1

Step 2: Now, inside the primary app, which is ‘website,’ make the following configurations. Open ‘urls.py’ and add the pattern to point to the ‘tutorial’ app. Import ‘include’ and add the path to the ‘urls.py’ file inside ‘tutorial’

Path(‘’,include(‘tutorial.urls’))

Step 2

Step 3: Go to ‘tutorial.urls.py’ and import ‘views’ as shown below. Add the app name for future reference and add the path to point to a homepage view for the tutorial app

Path(‘’,views.homepage, name=“homepage”)

Step 3

Step 4: But so far we don’t have any view called homepage. So, add a simple HttpResponse view called homepage. Don’t forget to import HttpResponse from django.http as shown below:

def homepage(request):
    return HttpResponse("First App")

Step 4

Step 5: Now, boot up the web server and run the following command

python manage.py runserver

boot up the web server and run the following command
Step 6: Open your browser location and go to the following location

Open your browser location and go to the following location
You will see the string that was passed as an HttpResponse being rendered over there. This means, we have successfully created our very first Django application.

Let us see what we learned so far in this Python Django tutorial for beginners.

What did we learn so far?

In this Django tutorial, we have covered the basic concepts of Django web framework, such as Django installation, creating Django web framework projects, creating Django applications, and the concepts of models, views, and templates. In addition, we built our very first web application using Django web framework. In our next section of this tutorial, we will cover the advanced concepts such as working with models, forms, databases, and more. See you there!

Course Schedule

Name Date Details
Python Course 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.