Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

How to Start a Django Project from Scratch: A Step-by-Step Guide for Beginners

Start new Django Project

Django is a robust open-source web framework based on Python language. It is used to build fast and secure web apps using Python. It was released in July 2005 and has since become popular. Now Django has several version updates. This article will teach you how to create a simple Django project from scratch in a few easy steps.

Prerequisites: Python3 should be installed in your system. If you do not have it, click here to follow the instructions.

Step 1: Create a Python environment

Python environment is a set of tools/libraries used in a project. It is isolated from other installed libraries in the system. Python recommends creating a separate environment for each different project as it helps prevent dependencies and version conflicts across projects. To create a Python environment all you have to do is navigate to your project folder in your terminal and execute the following command:

python3 -m venv <enviroment name>

For ease you can just name it ‘env’ and your command will be like this:

python3 -m venv env

This will generate a Python environment folder in your current directory, which will include all libraries related to this project.

Step 2: Activate Python Environment

To activate the environment you just created, go to the terminal and execute the following command:

For Windows:

<enviroment name>\scripts\activate

For example, type the following command in the terminal and press enter:

env\scripts\activate

For Linux or Mac:

source <enviroment name>/bin/activate

For example, type the following command in the terminal and press enter:

source env/bin/activate

It should look like this. At the moment our environment should be empty, and no package should be installed in it. To check this, you can run the following command, and it should return nothing.

pip freeze

For example:

Step 3: Install Django using pip:

Now we need to install a Django package in our virtual environment, this package will help us start a Django project. For this, the following command:

pip install django

If we run ‘pip freeze’ now, we can see that Django along with some necessary packages is being installed in our Python environment.

To confirm our installation, we can run the following command:

django-admin

We can see a list of commands supported by the Django package, which we just installed.

Step 4: Start a Django Project

So far, we have created our Python environment and installed our main package i.e. Django. It’s time to start a Django project. To do this, just run the following command with the name of your project.

django-admin startproject <project name>

This command will create a new folder containing our new Django project.

We can open our project folder in any IDE, for example, VS Code. This way we can look up our project structure.

We can also do all the above steps using a VS Code integrated terminal.

Step 5: Start Development Server

To start Django’s built-in development server, navigate into the project folder using the terminal where the manage.py file resides and run the following command.

python manage.py runserver

Once our development server successfully ramped up, we can hit the provided localhost link. By default, Django uses port 8000. We can change to a different port using the command ‘python manage.py runserver port 8001’. Now, we will just hit localhost:8000 or 127.0.0.1:8000 in our browser.

Amazing, we are ready to fly. Our project has started and running successfully. In our next article, we will discuss how to set our first custom home page in our new Django project.

Also, check: