CHAPTER 1
What is Django?
Introduction
Python is one of the most versatile programming languages used in the industry today. Because of Python’s simplicity, a programmer gets to focus more on the solution to the problem than on the solution to be implemented. Since it is open-source, we have a huge collection of free and open-source libraries, frameworks, modules, etc. available to use. Django is one of them. Before moving forward, you must have an understanding of frameworks and what Django can offer so that the outcomes match your expectations.
In short, Django is a framework build on Python and it is used for web application development. It is available for free and is open source. You will learn about the basic concepts of a framework. You will know what a framework is and see how Django provides a solution to your web application development requirements.
Structure
- What is a framework?
- Why do we need a web framework?
- What are some famous web frameworks?
- Is learning Django worth it?
- What is a virtual environment?
- How to create and use a virtual environment?
- How to install Django?
- How to create a Django project?
- Overview of your Django project files
Objectives
- Understanding the concept of frameworks.
- Understanding the need for frameworks.
- Familiarizing with the benefits of Django.
- Understanding the concept of virtual environments.
- Learning to install Django.
- Creating a Django project
- Understanding of the file system of Django
Django overview
Django is famous for its ability to create web applications rapidly. How does this happen? Django has inbuilt middleware and other stuff needed to run a web application. All you need to do is focus on your application. Django very elegantly provides the settings and connections in a configurable manner. You can simply create your applications and configure their settings as per your requirement and you are good to go.
Django was developed in 2003 by web developers at Lawrence Journal-World newspaper, Adrian Holovaty, and Simon Willison. It was released publicly under a BSD license in July 2005. The framework is named after the guitarist Django Reinhardt. Now, the Django Software Foundation has been maintaining Django. This official website for Django is https://www.djangoproject.com/. This is where you can get all the latest updates about Django.
The best thing about Django is that it is very elegantly documented. It is easy to learn and implement. You can find the latest documentation at https://docs.djangoproject.com/en/2.2/. Currently, as of September 2019, Django 2.2 is the latest version. The other widely used version is Django 1.8. I would recommend that you stick to the latest version.
What is a framework?
Let us suppose you have a simple task of drawing a square of side measuring 5cm. You can take your instruments, measure 5cms, and draw your square. Now, if you are asked to draw 1000 such squares, will you follow the same process, not likely? You would create a square frame of the given measurements and use that frame to draw the 1000 squares.
A software framework is very similar to this. Software development has seen a significant history and the requirements of a kind of software are pretty much the same. In a nutshell, a web application needs authentication and authorization, homepage, database connectivity, HTML pages for frontend, CSS for styling, a URL mapper, and a controller to choose what action is to be performed based on the request, etc.
These needs of a software developer or in this case, a web application developer has been identified and the setup has been created (like the frame in the squares example) where a web app developer can easily configure the settings (like the measurements in the squares example) and get the job done quickly and with ease.
Why do we need a framework?
The understanding of the framework makes it pretty much clear why we use frameworks. Let’s quantify the advantages of using a framework:
- It sets an industry-recognized software design structure. If every developer uses different designs, then it becomes difficult for the software owner or the client to maintain the software once the initial developer leaves the project. Thus, using a framework ensures that a standard design is followed.
- Re-writing code for common functionalities that are already available in frameworks does not make sense. Also, frameworks allow us to modify the functionalities as per our needs. So, customization is easy.
- The frameworks have been present for quite some time and extensively tested by developers worldwide, issues are reported, and bug fixes are continuously rolled. This ensures the software is bug-free.
- The frameworks keep updating with new features as the technology advances. Using a framework keeps your software updated. If you choose not to use a framework, then it becomes the developer’s responsibility to keep the software updated with the latest technology advancements.
Now, you are acquainted with what frameworks are and why we use it. Let’s take a look at what web frameworks are available in the market exception.
What are some famous web frameworks?
Apart from Django, there are several other web application frameworks available in the industry which you should be aware of which are as follows:
- Flask: Flask is a web application framework written in Python. Flask is based on the WSGI toolkit and Jinja2 template engine.
- Ruby on Rails: Ruby on Rails is a productive web application framework based on Ruby. One can develop an application at least quite faster with Rails than a typical Java framework.
- Angular: Angular is a framework on JavaScript by Google which helps us in building powerful web apps. It is a framework to build large scale and high-performance web applications while keeping them as easy-to-maintain.
- ASP.NET: ASP.NET is a framework developed by Microsoft, which helps us to build robust web applications for PCs, as well as mobile devices. It is a high-performance and lightweight framework for building web applications using .NET.
- Spring: Spring is the most popular application development framework for enterprise Java. Developers around the globe use Spring to create high-performance and robust web apps.
- PLAY: Play is one of the modern web application frameworks written in Java and Scala. It follows the MVC architecture and aims to optimize the developer’s productivity by using convention over configuration, hot code reloading, and display of errors in the browser.
Other frameworks worth mentioning are Laravel, Symfony, CodeIgniter, Express, React.js, Node.js, Hibernate, etc.
Is learning Django worth it?
Before you dive into Django and dedicate your time and effort to it, you should ask if it’s worth it. Is it used in industry? You will be glad to know that some of the most famous websites use Django. Examples: Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic, Open Knowledge Foundation, Pinterest, Open Stack, etc. So yes, learning Django is completely worth it. The best part is the more learn, the more it grows over you.
What is a virtual environment?
While developing software or web applications, you will be using a lot of pre-written code legally available from different sources. These sets of code are called libraries, packages, add-ons, plugins, etc. If you are using a framework, then that framework will also have a huge set of pre-written code which will help you implement different features in your application. To use these pre-written codes, you will need to install the libraries, plugins, etc. in your project/application.
The catch here is that different applications are expected to have different features. So, to implement those different features, you will need different set of frameworks, libraries, add-ons, plugins, etc.
Let’s assume you are developing two different applications on your personal computer. Project 1 is using Django 2.1, Python 3.5, bootstrap 4, etc. Whereas Project 2 is using Flask, Python 2.7, bootstrap 3, etc. Now, you have...