How to Install Django?
Django, a widely-used Python web framework, enables rapid web application development. It follows the Model-View-Template (MVT) architecture, not Model View Architecture, and offers built-in features such as authentication, authorization, an interactive admin dashboard, and security measures against SQL injection and CSRF attacks. Its scalability is evidenced by its use in major applications like Instagram and Dropbox, catering to millions of users daily.
How to Install Django?
We use pip, which is the package manager of python for the Django installation. But for that, we need to make sure that Python is installed in our system. We can check it by running the following command in the terminal
If this successfully returns the python version as an output, that means that python is installed in your system. If not, you can download it from here.
Install pip
By default, pip comes along with the python download, but we can run the following command to download and upgrade pip (if any update is available)
You can now check the working version of the pip in your system using the command
Prepare the Evironment
For better practice, we should work in a virtual environment, as it helps us to manage different versions of python packages because we might need a different version of the same package for different packages.
- Install virtualenv from the package manager pip at first
- Create a virtual environment providing it a name
Windows
Unix/macOS
env here is the name of the virtual environment, you can name whatever you want.
- Activate the virtual environment
For Windows
For Linux
Now, as our virtual environment has been successfully set up, we can proceed further to install Django.
Install Django
- Installing django
This will install the latest version of Django in your activated virtual environment.
Check Django Version
- Windows :
- Unix/macOS
If you have successfully installed Django, then this command will return you the version number
Setup a Django Project
To create a new Django project, we need to run the following command
A folder will be created with the name myProject, which is the name of your project(you can choose the name of your own choice).
This will create a folder structure as shown in the figure with the manage.py file in the root directory which executes your Django-specific tasks.
Start a Django App.
The following command will help you to create a Django application which is a kind of python package that you will use the most while working on a Django project
Make sure you run the command in the same directory where the manage.py file is located. myApp here is the name of your Django app.
Now as we have created the app, we need to include it in the INSTALLED_APPS section of the settings.py file in our project directory
This will let our project know that a myApp named application is created being a part of this project.
Reference Commands
Let's take a quick look at the reference commands that can be used for the Django development process
Action | Command |
---|---|
Create a virtual environment | python -m venv <env name> |
Activate the virtual environment | source <env name>/scripts/activate |
Install Django | pip insuall -U django |
Create a Django project | django-admin start project <project name> . |
Create a Django application | python manage.py startapp <app name> |
Django Architecture
Django follows the MVT architecture where,
M stands for Model V stands for View T stands for Template
In this architecture, the view contains most of the code and the logic part and interacts with both models and templates, where the model helps to connect with the database and return data to the views which can be passed to a template.
Benefits of Django Architecture
- Template is the component that makes MVT different from other architectures. These templates can be easily rendered and redirected through views
- Controllers, which are responsible for the user input and output is managed by Django itself.
- It is a loosely coupled architecture which means the changes in one component don't affect the others much.
Drawbacks of Django Architecture
- As compared to the MVC architecture, the URL is needed to be manually mapped here which sometimes gets trickier.
- As compared to MVC, understanding the flow of MVT architecture is comparatively difficult.
- It doesn't provide the feature of parallel development, which means we can not make many changes in the front end without making changes in the backend.
Conclusion
In this article, we learned about Setting up Django. Let us recap what we have learned so far.
- Django is a popular python based web framework that helps to create web applications quickly.
- It comes with many in-built functionalities like authentication, authorization, and security feature to protect from various threats like SQL injection.
- Install and set up the virtual environment on different systems like windows and macOS/Unix.
- Django can be installed by running the command pip install -U django in the terminal.
- Run django-admin startproject <project_name> . to start a new django project and python manage.py startapp <app_name> to initialise a new django app..
- Django follows the MVT architecture, i.e the Model View Template architecture.