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 are 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 set up 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
pip(python's package manager) but if you missed that, that's fine, you can install the get-pip.py 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 a python setup in your local machine. To check that Python was installed correctly, type in
python --version and
pip --version 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 setup.
Setting up Virtual Environment in python
Virtual Environment is software that 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
virtualenv that does this thing. It is for installing the Python-related packages into an isolated folder. So, we can install the
virtualenv 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
pip -m, 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:
venv can be anything like
env 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.
venv 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 anything installed in the prompt with the virtualenv activated will be isolated from the entire system and will be installed in the virtual environment. To activate the environment, we can use the command :
for Linux/macOS :
After this, your command prompt will have a
(venv) 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
startproject this is one of the management commands in Django. The django-admin is a command-line utility for doing the administrative tasks related to Django.
django-admin startproject myproject
myproject can be your project name. After this, you will see one new folder and one file pop up.
<project-name> folder and
manage.py file. We don't have to touch the
manage.py 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 the
djagno-admin command, but you need to set certain environment variables and modify the settings.py file as per the project-name. You can use the
django-admin as the steps given in the Django documentation.
The output of the command
python manage.py runserver should be visible in the browser at
https://127.0.0.1:8000 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 this:
$1 cd $1 pip install virtualenv virtualenv venv source venv/bin/activate pip install django django-admin startproject $1 . clearmkdir
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
save as commands.bat file
From this section, we were able to set up the Django project in our local system. In the next part, we will cover the
folder structure 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 work. So, thanks for reading and Happy Coding :)