How to Install Django?

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Django is a popular python based web framework that helps one to create web applications quickly. Let's take a sneak peek at how to set up a Django project and get started with it.

Scope

In this article, the below topics will be covered --

  • We will learn about installing pip, which is a python package library.
  • We will also be creating and activating a virtual environment.
  • We will learn how to install and check the version of Django.
  • We will also go through creating a Django project and start an application.
  • Finally, we will cover the MVT structure of Django along with its advantages and drawbacks.

Introduction

Django is based on MVT, which is Model View Architecture and it comes along with a lot of in-built features like authentication, authorization, interactive admin dashboard, and security features to prevent SQL and CSRF attacks. Django apps are scalable as popular web applications like Instagram and Dropbox are built on Django which are used by 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

ActionCommand
Create a virtual environmentpython -m venv <env name>
Activate the virtual environmentsource <env name>/scripts/activate
Install Djangopip insuall -U django
Create a Django projectdjango-admin start project <project name> .
Create a Django applicationpython 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.