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.
In case you want to jump right into a particular topic, here’s a table of contents for this Django tutorial.
First of all, watch this video on ‘Django for Beginners’:
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.
Wish to crack Django job interviews? Intellipaat’s Top Django Interview Questions are meant only for you!
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.
Django Installation Process
Check if Python is installed in the system by using the following command:
If Python is not installed in your system, go through this tutorial on ‘How to Download and Install Python?’
Step 1: Open the command prompt
Step 2: Run the pip command
pip install django
Architecture of Django Web Framework
Django web framework follows the MVT (Model View Template) architecture. This architectural pattern is designed for easy and rapid web development.
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.
Watch this video on ‘Python Django Tutorial’:
Any queries related to Django? Join our Community and clear your queries now!
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.
Creating a Project
In order to create a new Django project, run the following django-admin startproject command in the command prompt.
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.
Inside the ‘website’ directory (root directory), you will see another directory with name same as the root directory.
Inside that directory the following default python files are created.
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.
Interested in learning Django? Enroll in our Django Course now!
Creating an Application
- Open the command prompt
- Navigate to the directory where you have created the project
Here, you will see what difference this command makes inside the project directory. There is a new folder called ‘tutorial’ inside the ‘website’ directory.
Now, if you go inside the ‘tutorial’ application folder, you will see the following Python files Django created for you to configure the application.
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 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’
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
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:
return HttpResponse("First App")
Step 5: Now, boot up the web server and run the following command
python manage.py runserver
Step 6: 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.
For more advanced concepts on Django Web Framework and Django REST API, refer to this structured Python Django Training and Certification Course!
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!