Django Introduction and Installation
At 6/2/2023
What is Django?
Django is a Python-based web framework that allows you to quickly create web applications without all of the installation or dependency problems that you normally will find with other frameworks.
When you're building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use.
Why Django?
- It's very easy to switch databases in the Django framework.
- It has a built-in admin interface which makes it easy to work with it.
- Django is a fully functional framework that requires nothing else.
- It has thousands of additional packages available.
- It is very scalable.
Popularity of Django
Django is used in many popular sites like Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic, etc. There are more than 5k online sites based on the Django framework. ( Source )
Sites like Hot Frameworks assess the popularity of a framework by counting the number of GitHub projects and StackOverflow questions for each platform, here Django is in 6th position. Web frameworks often refer to themselves as “opinionated” or “un-opinionated” based on opinions about the right way to handle any particular task. Django is somewhat opinionated, hence delivering the in both worlds( opinionated & un-opinionated ).
Features of Django
Versatility of Django
Django can build almost any type of website. It can also work with any client-side framework and can deliver content in any format such as HTML, JSON, XML, etc. Some sites which can be built using Django are wikis, social networks, new sites, etc.
Security
Since the Django framework is made for making web development easy, it has been engineered in such a way that it automatically does the right things to protect the website. For example, In the Django framework instead of putting a password in cookies, the hashed password is stored in it so that it can’t be fetched easily by hackers.
Scalability
Django web nodes have no stored state, they scale horizontally – just fire up more of them when you need them. Being able to do this is the essence of good scalability. Instagram and Disqus are two Django-based products that have millions of active users, this is taken as an example of the scalability of Django.
Portability
All the codes of the Django framework are written in Python, which runs on many platforms. Which leads to run Django too in many platforms such as Linux, Windows, and Mac OS.
Installation of Django
Install Python3 if not installed in your system (according to the configuration of your system and OS) from here. Try to download the latest version of pPythonit's pPython3.11.0 this time.
Note- Installation of Django in Linux and Mac is similar, here I am showing it in Windows for Linux and Mac just open the terminal in place of the command prompt and go through the following commands.
1 - Install pip - Open the command prompt and enter the following command.
python -m pip install -U pip
2 - Install and create virtual environment-Enter following command in cmd.
python -m venv venv
3 - Activate virtual environment by runing this commande in cmd.
.\venv\scripts\activate
4- Install Django- Install Django by giving the following command.
pip install Django
5 - Start a project by following the command.
Django-admin start project freesad_project .
This command will create a Django project, freesad_project is the name of the project, and the dot means that the project will be created in the current directory.
6 - Now we need to create new django app by the following command.
python manage.py startapp app_name
7 - Start the server- Start the server by typing the following command in cmd.
python manage.py runserver
To check whether the server is running or not go to the web browser and enter http://127.0.0.1:8000/ as URL.
Benefits of Django Architecture.
- Rapid Development
- Loosely Coupled
- Ease of modification
Drawbacks of MVC Architecture.
- Too much load on Model Component
- Development Complexity is high
- Two components are controlling View