Django Basics: Setup and Installation
The crucial aspect of starting to learn any framework is the ease to set it up and Django by far is the easiest of the options out there. There is just a few li#django #python #web-development
The crucial aspect of starting to learn any framework is the ease to set it up and Django by far is the easiest of the options out there. There is just a few lines of code to install django if you already have python installed in your system. In this article, we see how to setup a django project along with a virtual environment.
If you already have python and pip installed, you can move on to the virtual environment setup .
Installing Python and PIP
Django is a python based framework so that makes sense to have Python installed along with its package manager to use Django.
To install Python, you can visit the official Python website to download any relevant version for your system (recommended 3.7 and above).
Mostly the Python installation comes with the option to install
(python's package manager) but if you missed that, that's fine, you can install the
file into your system and run the below code:
Make sure the include the relative path to the file if you are not in the same folder as the file.
So, that should be python setup in your local machine. To check that python was installed correctly, type in
to check if they return any version number. IF they do, Congratulations !! You installed Python successfully and if not, don't worry there might be some simple issues that can be googled out and resolved easily.
Let's move on to the actual setting of the Django project set up.
Setting up Virtual Environment in python
Virtual Environment is a software which isolates the installation of dependencies and libraries for a specific project, making it a clean and safe environment for deployment as well as maintenance.
In Python, we have a virtual environment package known as
that does this thing. It is for installing the Python related packages into a isolated folder. So, we can install the
package in python by following the following steps:
Firstly, install the virtual environment package, it's not mandatory but it keeps things simple and easy for your project in correspondence to the entire OS. So in python, we have a module to create the virtual environment pretty easily,
pip install virtualenv
You can use
, or however you install normal python modules. This just installs the python virtual environment, we need to create a virtual environment in the current folder.
Creating a virtual environment
We need to create the environment so as to give the Python interpreter an indication to consider the current folder as an isolated Python environment. We need to create a virtual environment in the current folder, so for that navigate to the folder where you want to create the project and enter the following command:
can be anything like
just for your understanding and simplicity it's a standard name kept for the same. After this, you will see a folder of the same name i.e.
or any other name you have used. This is the folder where python will keep every installation private to the local folder itself.
Activating Virtual environment
Now, we need to activate the virtual environment, this means that any thing installed in the prompt with the virtualenv activated will be isolated from the entire system and will be installed on in the virtual environment. To activate the environment, we can use the command :
for Linux/macOS :
After this, your command prompt will have a
attached in the beginning. This indicates you are in a virtual environment, things you do here, may it be module installation or any configuration related to python will stay in the local folder itself.
After the virtual environment is set up and activated, you can install Django and get started with it. Django is a python module or package, which can be easily installed using its package manager
Install Django using pip:
pip install django
Create a Django Project
After the installation is completed, you can start a Django project in the current folder from the django package we installed. There are several commands available in the django module which you can execute in the command line that we'll discuss later.
For now, we will use the command
this is one of the
in Django. The
is a command line utility for doing the administrative tasks related to Django.
django-admin startproject myproject
can be your project name. After this, you will see one new folder and one file pop up.
file. We don't have to touch the
file but we use it in most of the commands to use the Django functionalities, it is quite similar to the
You can now run your basic server using the command :
python manage.py runserver
You can use
command, but you need to set certain environment variables and modify the settings.py file as per the project-name. You can use the
as the steps given in the django
The output of the command
python manage.py runserver
should be visible in the browser at
as below :
That's it the base django project is installed in your system. To stop the server simply press
Follow the below GIF for a clear understanding of those instructions:
You can avoid manually typing the commands once you get the idea of the process in setting up a django project by executing a simple shell script (for Linux/macOS) or a batch script (for Windows). The script looks something like:
#!/usr/bin/env bash mkdir $1 cd $1 pip install virtualenv virtualenv venv source venv/bin/activate pip install django django-admin startproject $1 . clear
Save as commands.sh file
mkdir %1 cd %1 pip install virtualenv virtualenv env call env\Scripts\activate pip install django django-admin startproject %1 . cls
For further instructions you can checkout the GitHub repository or a detailed article about it.
From this section, we were able to setup the Django project in our local system. In the next part, we will cover the
of the Django project. We won't directly go into the code part because that is very easy once you understand the flow of the framework and its internal working. So, thanks for reading and Happy Coding :)