Creating First Django Project

Now that you know what Django is, it's time to get your hands dirty! In this lesson, we will install Django, create your very first project, and run it on your computer. Think of it like setting up a brand-new kitchen before you start cooking — we need to arrange the tools and workspace first.

Prerequisites (What You Need Before Starting)

Before creating a Django project, make sure the following tools are already installed on your computer:

  • Python (version 3.8 or above): Django is built on Python, so Python must be installed first. Check by running python --version in your terminal.
  • pip: This is Python's package installer. It usually comes with Python automatically. Check by running pip --version.
  • A Terminal / Command Prompt: On Windows use Command Prompt or PowerShell. On Mac/Linux use the Terminal.
Simple Analogy: Before baking a cake, you need a working oven (Python) and a way to bring ingredients home (pip). Without these two, you can't even begin.

Create a Virtual Environment

A Virtual Environment is like a private room for your project. It keeps your project's dependencies (like Django) separate from other Python projects on your computer. This way, two different projects can use two different versions of Django without fighting each other.

Run the following commands one by one in your terminal:


# Create a virtual environment named "myenv"
python -m venv myenv

# Activate it on Windows
myenv\Scripts\activate

# Activate it on Mac / Linux
source myenv/bin/activate

After activation, you will see (myenv) at the beginning of your terminal line. That means your private room is now active and ready.

Example: Imagine you are working on two food projects — one needs "salt only" and another needs "no salt at all". A virtual environment makes sure each project gets exactly what it needs without mixing them up.

Install Django

With your virtual environment active, install Django using pip. This downloads Django from the internet and places it inside your virtual environment.


pip install django

Once the installation is complete, confirm Django is installed by checking its version:


python -m django --version

You should see a version number like 5.0.x printed on the screen. That means Django is ready to use!

Example: Installing Django is just like downloading a new app on your phone. Once it says "Installed successfully", it's ready to open and use.

Create Your First Django Project

Now we will use Django's built-in command to create a new project. Let's name our project myschool (imagine we are building a school management website).


django-admin startproject myschool

This single command creates a new folder called myschool containing all the default files Django needs to get started. Now move inside that folder:


cd myschool
Example: Running startproject is like ordering a "starter home kit". When it arrives, all the walls, doors, and electricity are already set up. You just need to move in and decorate it for your needs.

Understanding the Project Structure

After running startproject, Django creates a set of files automatically. Here is what the folder structure looks like and what each file does:


myschool/                  <-- Your outer project folder (root folder)
│
├── manage.py              <-- The project's remote control
│
└── myschool/              <-- Inner folder (same name as project)
    │
    ├── __init__.py        <-- Tells Python this is a package
    ├── settings.py        <-- All project settings and configurations
    ├── urls.py            <-- The URL map / GPS of your website
    ├── asgi.py            <-- Entry point for ASGI (async) servers
    └── wsgi.py            <-- Entry point for WSGI (traditional) servers

manage.py

This is the main controller of your Django project. You use it to run the server, create apps, apply database changes, and many more tasks. You will type commands with this file almost every day.

  • Example: Think of manage.py like the TV remote control. You press different buttons (commands) on it to control what happens on your TV (project).

settings.py

This file controls all the settings of your project — like which database to use, which apps are installed, where your HTML templates are stored, and more. It is the single most important configuration file in the entire project.

  • Example: settings.py is like the Settings menu on your smartphone. It decides the language, notifications, display, and security for the whole phone.

urls.py

This file contains the list of all URL addresses your website understands. When someone types a URL like /home or /contact, Django looks here to find out which page to show them.

  • Example: urls.py is like a reception desk at an office building. A visitor says "I want to go to Finance Department" and the receptionist checks the list and says "Go to floor 3, room 301".

__init__.py

This is an empty file. Its only job is to tell Python: "Hey, treat this folder as a package." You will almost never need to edit this file.

wsgi.py and asgi.py

These are files used when you deploy your project to the internet (host it on a live server). For now, during learning, you don't need to touch these files at all.

Run the Development Server

Django comes with a built-in lightweight web server just for development (testing on your own computer). Let's start it now:


python manage.py runserver

You will see output like this in your terminal:


Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s)...
Django version 5.0, using settings 'myschool.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Now open your browser and go to: http://127.0.0.1:8000/

You should see the default Django welcome page — a rocket launching! This means your Django project is running successfully on your computer.

Example: Running runserver is like pressing the Power button on your new TV to check if everything is working before hanging it on the wall. The welcome page is Django's way of saying "I'm alive and ready!"

Stop the Development Server

To stop the server at any time, simply press CTRL + C in the terminal. The server will shut down immediately.

Quick Recap — Complete Commands in Order

Here is a clean summary of all the commands you used from start to finish:


# 1. Create a virtual environment
python -m venv myenv

# 2. Activate the virtual environment (Windows)
myenv\Scripts\activate

# 3. Install Django
pip install django

# 4. Create a new Django project
django-admin startproject myschool

# 5. Move into the project folder
cd myschool

# 6. Run the development server
python manage.py runserver

# 7. Open browser and visit
# http://127.0.0.1:8000/

Common Errors and Fixes

"python is not recognized as a command"

This means Python is not installed or not added to your system's PATH. Re-install Python from python.org and make sure to check the box that says "Add Python to PATH" during installation.

"django-admin is not recognized"

This usually means your virtual environment is not activated. Make sure you see (myenv) in the terminal before running Django commands.

Port 8000 already in use

Another program may be using port 8000. You can start the server on a different port like this:


python manage.py runserver 8080

Then visit http://127.0.0.1:8080/ in your browser.

Leave a Comment

Your email address will not be published. Required fields are marked *