In this section, you will find information about what you need to do before starting to develop codes with Django.

Install Python

Django is a web framework written In Python. Therefore, the first thing you need to do is to install Python in your system. In order to install Python, click here and use website guidelines to install Python in your system. Make sure to add the Python in your system path during Installation (there is a checkbox which you need to mark it to add the Python to system paths). In addition, make sure to install pip during Python installation.

Install Git (Git-Bash)

You need command line tools to work with Python and Django. We will use bash as our shell language. In addition, you need to install Git in your system. Visit here to install Git in Linux or Git-bash if you are a Windows OS user.

In these tutorials we use Windows OS and git-bash. However, the commands are similar in all Operating Systems.

Install Text Editor

We will used VS Code to develop our codes. VS code is a free open source text editor which is available in all Operating Systems. It has many extensions to develop Python, HTML, CSS and etc. In addition, it has an integrated terminal and you can access to git-bash or other terminal wihtout leaving the editor environment.

Virtual environment

In order to develop a django project, we will create a virtual environment everytime we start a new project. Virtual environment is a tool by which you install your libraries without polluting your other environment.

Create Virtual environment

Open Git-bash (or cmd or powershell) to create a Python virtual environment as follows:

javad@DESKTOP-2GOSU3N MINGW64 ~
$ mkdir first_django_project

javad@DESKTOP-2GOSU3N MINGW64 ~
$ cd first_django_project

javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ python -m venv .venv

The first line generates a directory named first_django_project. Then, we open this directory. Inside the directory, we run python -m venv .venv which creates the virtual environment in a directory named .venv. The structure of directories is shown in the below image:

directory structure
Fig.1 - Directory structure after creating virtual environment.

Activate Virtual Environment

After you create the virtual envorinment, you need to activate it to install and import libraries. Inside the .venv directory, there must be three folders: Lib, Include, and Scripts or bin. In order to activate virtual environment, type the following:

javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ source .venv/Scripts/activate
(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$

The first line in above code activates the virtual environment. After that, you must see the (.venv) above all prompt lines in the git-bash terminal.

Install Django

Now, that you have setup the virtual environment and activated it, it's time to install django. When you install a library in virtual environment it will be installed inside .venv/Lib/site-packages directory.

To install django in your virtual environment write:

(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ python -m pip install django

Afther installation is done, you can find the Django source code inside .venv/Lib/site-packages/django directory. Now, run the following to see the version of Django in your virtual envoironment:

(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ django-admin --version
4.0

For the version of Django is 4.0. What is the version of your Django?

.

Initialize Git

In order to keep track our changes in codes will use Git. To initailize Git in this directory, execute git init. This commands will create .git directroy and setup git database to keep track of changes in your source code.

(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ git init
Initialized empty Git repository in C:/Users/javad/first_django_project/.git/

Now, wee need to add a .gitignore file in the directory to determine which kind of files or directories should not be tracked by Git. The virtual environment files should not be tracked since they are not part of our code, we just need to keep the information about version of libraries we have used in our source code. In order to do that, we use pip freeze > requirements.txt.

Next we create .gitignore file and add the files and directories which needs to be ignored by Git:

.venv/

__pycache__/

*.pyc

Fig.2 - Add these lines to .gitignore file.

Strcute of directories after these commands will look like this:

directory structure (git)
Fig.3 - Directory structure after initializing git repo. The black boxes shows the content of each file. None that the content of your requirements.txt may be different than the one shown in this figure.

Next, we create our first commit in Git repository

(.venv)
javad@DESKTOP-2GOSU3N MINGW64 ~/first_django_project
$ git add -A
$ git commit -m "created virtual environment and initialized git repo"

Video Lecture

You can watch the video lecture for this lesson (The language of the video is Farsi)

Summary

Congratulations! You have just finished installing django and initializing a Git repo for your project. In the next section, we are going to create our first django project.