How to create a new Django project: step by step guide
Arika O
Posted on October 7, 2020
This applies to Windows users. I will be using the cmd
and the VS Code Terminal for writing my commands. Make sure you have Python
, pip
(Python's package manager - just like npm
is for Javascript) and Visual Studio Code
installed on your machine and the Python extension enabled in VS code. I will assume you are familiar with Python and with the command line/ how to write basic commands.
What are we going to achieve:
- Install the virtual environment (I'll explain what that is in a bit)
- Activate/ deactivate the virtual environment (using the
cmd
first and VS Code after) - Install Django
- Create a new Django project
INSTALLING THE VIRTUAL ENVIRONMENT AND WORKING WITH IT
STEP ONE: Navigate to the folder where your Django project and all its dependencies will reside and install the virtual environment
.
A virtual environment (also called a
venv
) is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments. At its core, the main purpose of Python virtual environments isto create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.
I have a folder on my desktop called DjangoProjects so I'm going to open a cmd
inside it. So you do the same: open your folder, click on the location bar of Windows Explorer and type cmd
. A command line window should appear so after that happens type pip install virtualenv
like so:
C:\Users\Silvia\Desktop\DjangoProjects>pip install virtualenv
This will install the virtual environment
but it won't activate it (we will do this in a bit). In the console, you should see a message saying Successfully installed virtualenv + the version number
. We can create and activate as many venvs
as we want from now one with the condition that they all stay in this folder (in my case in DjangoProjects).
STEP TWO: Create a folder for your new Django project. In my DjangoProjects folder I will create a new directory called my_website
so I'll type in the cmd
:
C:\Users\Silvia\Desktop\DjangoProjects>mkdir my_website
STEP THREE: Navigate into the newly created folder:
C:\Users\Silvia\Desktop\DjangoProjects>cd my_website
STEP FOUR: The folder is now empty so let's create a virtual environment
inside it (the venv we create now will only work in this particular directory). I will call my venv env1
(the name is random, you can call it whatever you want). The command for creating a venv is py -3 -m venv
+ the name of the environment.
C:\Users\Silvia\Desktop\DjangoProjects\my_website>py -3 -m venv env1
STEP FIVE: The virtual environment has been created but we don't really get any response in the console telling us this so to check it, open your project folder and check its contents (or type dir
in the cmd
and you'll what's inside your directory). You'll should see a single folder with the name of your venv. Let's navigate inside it:
C:\Users\Silvia\Desktop\DjangoProjects\my_website>cd env1
The env1
folder should have two directories and two files inside it and should look something like this:
To activate the virtual environment we'll have to use the activate.bat
file inside the Scripts
folder and type the activate.bat
command. I've seen this done in different ways but many times it happens that I get lost with all the slashes I need to type for the complete path so the way I do it is by navigating in the Scripts
folder and run the activate.bat
command directly there. So we'll have to do something like:
C:\Users\Silvia\Desktop\DjangoProjects\my_website\env1>cd Scripts
C:\Users\Silvia\Desktop\DjangoProjects\my_website\env1\Scripts>activate.bat
Now we'll see something like this in the cmd
:
(env1) C:\Users\Silvia\Desktop\DjangoProjects\my_website\env100\Scripts>
So, the que for you to know that the venv
is activated is to see its name in parentheses before the path, in the console. The way to deactivate
the venv is by typing deactivate
in the command line while being inside the Scripts
folder:
(env1) C:\Users\Silvia\Desktop\DjangoProjects\my_website\env1\Scripts>deactivate
Now that we've installed and created the venv and learned how to activate and deactivate it, we'll move to VS Code to install Django and create a project.
WORKING IN VS CODE, INSTALLING DJANGO AND CREATING A PROJECT
STEP SIX Open you project folder with VS Code. At this point, the only thing you have inside it is the virtual environment directory.
Let's activate the venv
. For this, go to View -> Command Pallete -> Python: Select Interpreter
. You will see a list of options. You should pick the one mentioning your virtual environment in it.
Now, if you open a new Terminal (go to Terminal -> New Terminal), you will see the name of the environment in round parentheses, so this mean it is activated.
(env1) PS C:\Users\Silvia\Desktop\Django\my_website>
STEP SEVEN
Let's now install Django. Type in the Terminal pip install django
.
(env1) PS C:\Users\Silvia\Desktop\Django\my_website> pip install django
It will take a while for the installation to complete and when it's done you can type python -m django --version
in the terminal to see if the installation was successful and what version of Django are you using. At the time I'm writing this, I am using Django 3.1.2
.
STEP EIGHT
Finally, let's create a Django project. For this, we must use the django-admin startproject
+ the desired name for the project command. I will call mine my_blog
.
(env1) PS C:\Users\Silvia\Desktop\Django\my_website>django-admin startproject my_blog
The projects gets created almost instantly and if you now look in the Explorer window in VS Code, you will see a new folder with the name you picked for your project.
Upon opening that folder, you will see another folder with the same name and a file called manage.py
.
I'm not going to go into details but all you need to know is that this file can be found in every root directory of a new Django project and it helps with running commands. So, a lot of times you'll be writing something like python manage.py + some command
.
The nice thing about Django is that it comes with a dedicated server so we can start writing and testing our code right away (we're going to run an emulated server on our local machine). The way we start the server is by using the python manage.py runserver
command:
(env1) PS C:\Users\Silvia\Desktop\Django\my_website\my_blog> py manage.py runserver
Important: you must run this in the ROOT of your Django project, where manage.py
lives, otherwise you'll get an error (notice I navigated into the my_blog
folder).
When this is done, you'll get a message in the terminal saying Starting development server at http:// + some IP address
. Copy and paste that address into your browser and if everything went well, you should see something like:
Congratulations, you have successfully created a new Django project 😊.
Image source: Hitesh Choudhary/ @hiteshchoudhary on Unsplash
Posted on October 7, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.