DEV Community

Cover image for How to create a new Django project: step by step guide
Arika O
Arika O

Posted on • Edited on

How to create a new Django project: step by step guide

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:

  1. Install the virtual environment (I'll explain what that is in a bit)
  2. Activate/ deactivate the virtual environment (using the cmd first and VS Code after)
  3. Install Django
  4. 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 is to 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

STEP THREE: Navigate into the newly created folder:

C:\Users\Silvia\Desktop\DjangoProjects>cd my_website
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The env1 folder should have two directories and two files inside it and should look something like this:

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

Now we'll see something like this in the cmd:

(env1) C:\Users\Silvia\Desktop\DjangoProjects\my_website\env100\Scripts>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

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.

Alt Text

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> 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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    
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

Upon opening that folder, you will see another folder with the same name and a file called manage.py.

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

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:

Alt Text

Congratulations, you have successfully created a new Django project 😊.

Image source: Hitesh Choudhary/ @hiteshchoudhary on Unsplash

Top comments (8)

Collapse
 
corentinbettiol profile image
Corentin Bettiol • Edited

Maybe using the recommended use of pip is better than just calling pip (we don't know the pip/python version from this call and it may change with system/python updates) ?

Example:

python3 -m pip install venv
python3 -m venv .venv
.venv/bin/activate
python3 -m pip install django
Enter fullscreen mode Exit fullscreen mode

(also I use venv since it's the default way of creating virtualenvs using Python3)

Collapse
 
arikaturika profile image
Arika O

Sure, we can do things multiple ways. If you're concerned with the version, your approach is better suited. Thank you for mentioning it.

Collapse
 
stokry profile image
Stokry

Thanks for sharing!

Collapse
 
arikaturika profile image
Arika O

You're welcome, hope it helped!

Collapse
 
christo26 profile image
Christopher Regan

Thank you.

Collapse
 
arikaturika profile image
Arika O

Welcome, thx for the feedback.

Collapse
 
yokotobe profile image
yokotobe

Thank you

Collapse
 
arikaturika profile image
Arika O

You are welcome, I hope it helped :)!