Introduction to Django Framework
Django is a Python-based server-side web-framework used worldwide to make web applications. Some of the top companies using Django are:
- Google (Youtube)
Since Django employs Python, we can not only deploy regular websites but also Machine Learning Models such as for Speech Recognition, Fraud Detection, etc over the web. Django has a very modular approach called as MVT (Model-View-Template) architecture. Following this architecture makes the code very easy to develop, deploy and maintain.
Django also provides a built-in Admin module that can be used to store master values in the website. For example, for processing a Loan application, there may be a screen called “New Loan”, then in this form suppose there is a drop-down menu for Loan Types, Personal Loan, Car Loan, Home Loan, etc. then these values are maintained through the admin panel.
Understanding Django Framework
Now as we know that Django has an MVT architecture, we will go step by step in explaining each term.
The moment we install the Django framework, and create a new project, following files (the most important and frequently) are automatically created for us to write our code :
Each of these files is intended for code modularity. settings.py file stores all the custom configurations such as database connection parameters, server details on which the application will be hosted, global variables that can be accessed throughout the application, etc.
urls.py file has a list of all the custom URLs which will be used in the application to access different pages. Let us look at the models, views, and templates.
Model in Django refers to Database Tables which are created as per the requirements to store data. For example, if we want to create a Bank website, then we need tables to store customer data such as Customer Name, Account Number, Bank Balance, etc. The code for writing these models is put into models.py file.
Now you may wonder which database to use for creating Django Models. Relax! Django supports all major databases (Oracle, PostgreSQL, MS SQL Server, MySQL ) and you need not worry about different types of SQLs for different databases. Because Django implicitly can translate your Python code into corresponding Database SQL.
View in Django refers to what needs to be done / processing over the requests which are received from the client to the server. These http requests (GET, POST, etc) can be for specific purposes such as Account Debit, Account Credit, Create a new account, etc.
Views, in turn, interact with models to store/retrieve/delete data. The code for writing these views is put into views.py
Templates refer to the HTML pages which the user sees. It contains the background images, TextBoxes, Buttons, etc which are rendered in the browser of the client and give the look and feel of the website.
Templates don’t have a .py file because templates are HTML codes. Hence we explicitly have to make a templates folder and keep all the .html files into this folder.
Why Use Django?
Django is popularly known as “Web framework for perfectionists with deadlines”. This is a very apt saying because all the components you may require to build a web application comes bundled with this framework viz. in-built web server and SQLite Database. Also, the admin panel just prevents reinventing the whole wheel and the developer can focus on writing the core application code rather than creating basic necessities.
The other most important feature of Django is “SECURITY”. Django forces the developer to implement security-related features into the application, otherwise, the code does not execute successfully. For example csrf (Cross-Site Request Forgery). csrf is a random token that is generated every time we submit form data. This token is validated by the Django server on every Http POST Request it receives. If some hacker tries to attack, then this token becomes invalid and our application remains secure.
Another interesting feature of Django is – Template extensions and Tags. Django provides with tons of template tags that can be used directly in the templates (.html codes) thus preventing development effort and reducing the size of templates. One template (for ex: base.html) can be easily extended into another template and as a result, this other template reduces to only a few lines of html code.
Importance of Django Framework
Up till now, you must have got a fair idea as to how Django can be so much useful. The importance of Django can be realized from the fact that the world’s top companies use Django for their application. This includes Mozilla, NASA, Instagram, Pinterest, Dropbox and many more. The market is full of Django opportunities, yet only a few people are available hence Django people easily get an upper hand.
Moreover, cloud service providers such as AWS have a dedicated service to host Django applications called Elastic Beanstalk which provides managed Apache server, PostgreSQL, Load Balancer and AutoScaling for Heavy Load applications. Django applications can also be exposed as APIs for consumption by Third Party Softwares. This is done by using the Django REST Framework.
Python is the single most important prerequisite for learning Django. More specifically, Concepts related to Class, Inheritance, Functions, Data Structures (Lists, Dictionary, Tuples) should be very much clear because the entire Django framework revolves around Classes and Functions.
Django is extremely rapid in development, scalable and flexible in design and highly secure and reliable. The vast Django community provides a solution to any problem you might face during Django development. The statement if you practice and master the basic concepts of Django, then you can build Production-Ready web applications in a matter of a few days.
This is a guide to the Django Framework. Here we discuss the understanding, importance, use, and prerequisite of the Django Framework. You may also look at the following articles to learn more –