Introduction to Django

Reviewed & published by Brayan K

Django is a high-level Python web framework for building database-driven websites quickly, giving you routing, a database layer, templating, an admin, and security tools out of the box so you can focus on your application's features instead of reinventing the basics.

Part of the free Django course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

In this first lesson you'll learn what Django is, why developers reach for it, and how to install it and spin up your very first project.

What You'll Learn in This Lesson

1 What Is Django?

Django is a free, open-source web framework written in Python. A web framework is a toolkit that provides the common machinery every website needs — receiving HTTP requests, talking to a database, generating HTML, and sending responses back to the browser.

Django follows the MTV pattern — Model, Template, View — which is its take on the classic MVC architecture:

Because Django is "batteries included," it ships with an ORM, an automatic admin site, form handling, authentication, and protection against common web attacks. You write Python; Django handles the plumbing.

# Django is written in Python — let's confirm Python works first.
# This is the kind of view function you'll write throughout the course.

def home_view(request):
    """A view takes a request and returns a response."""
    site_name = "My First Django Site"
    visitor = "Alice"
    response_text = f"Welcome to {site_name}, {visitor}!"
    return response_text   # In real Django you'd return HttpResponse(response_text)

# Simulate Django calling your view with a fake request object
fake_request = {"path": "/", "method": "GET"}
print(home_view(fake_request))
print("Request path was:", fake_request["path"])

# Django maps a URL like "/" to a view like home_view for you.

# ✅ Expected output:
# Welcome to My First Django Site, Alice!
# Request path was: /

2 Installing Django

Django is installed with pip, Python's package manager. It is best practice to install it inside a virtual environment so each project keeps its own isolated dependencies.

# 1. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate        # On Windows: venv\Scripts\activate

# 2. Install Django
pip install django

# 3. Confirm the installed version
python -m django --version      # e.g. 5.0.6
# You can check whether Django is importable and which version you have
# from inside Python itself:

import sys

try:
    import django
    print("Django is installed!")
    print("Version:", django.get_version())
except ImportError:
    print("Django is not installed yet.")
    print("Run: pip install django")

# Django 5.2 needs Python 3.10 or newer, so that is the check worth making:
print("Python 3.10 or newer:", sys.version_info >= (3, 10))

# In the real course you'd run these commands in your terminal, not here,
# but this shows how Django reports its own version.

# ✅ Expected output:
# Django is installed!
# Version: 5.2.17
# Python 3.10 or newer: True

3 Creating and Running a Project

The django-admin startproject command generates the scaffolding for a new project. Then manage.py runserver starts a local development server.

# Create the project named "mysite"
django-admin startproject mysite
cd mysite

# Start the development server
python manage.py runserver

# Open http://127.0.0.1:8000/ — you'll see Django's rocket welcome page!

The command creates this folder structure:

mysite/
    manage.py            # Command-line utility for your project
    mysite/
        __init__.py
        settings.py      # All project configuration lives here
        urls.py          # The root URL routing table
        asgi.py          # Entry point for ASGI servers
        wsgi.py          # Entry point for WSGI servers
# manage.py is just a thin Python script. Here's a simplified version of
# the logic it runs so you can see there's no magic — it reads the command
# you typed and dispatches it.

import sys

def main(argv):
    commands = {
        "runserver": "Starting development server at http://127.0.0.1:8000/",
        "migrate": "Applying database migrations...",
        "startapp": "Creating a new app...",
    }
    if len(argv) < 2:
        print("Usage: python manage.py <command>")
        return
    command = argv[1]
    print(commands.get(command, f"Unknown command: {command}"))

# Simulate: python manage.py runserver
main(["manage.py", "runserver"])
main(["manage.py", "migrate"])

🎯 Your Turn: Name Your Project

Fill in the blank so the function builds the correct startproject command for a project called blog.

# 🎯 YOUR TURN: replace ___ with the project name "blog"

def build_command(project_name):
    return f"django-admin startproject {project_name}"

project = "___"
print(build_command(project))

# EXPECTED OUTPUT:
# django-admin startproject blog

Common Errors (And How to Fix Them)

❌ command not found: django-admin

Django isn't installed or your virtual environment isn't active.

✅ Fix: activate the venv and run pip install django again.

❌ That port is already in use

Another process is using port 8000.

✅ Fix: run on a different port: python manage.py runserver 8080

❌ ModuleNotFoundError: No module named 'django'

You're using a Python interpreter where Django isn't installed.

✅ Fix: make sure your virtual environment is activated before running commands.

📋 Quick Reference

CommandWhat It Does
pip install djangoInstall the Django package
django-admin startproject mysiteCreate a new project
python manage.py runserverStart the development server
python -m django --versionPrint the installed Django version

🏆 Mini Challenge: Project Bootstrapper

Write a helper that prints the full sequence of commands needed to start a brand-new Django project from scratch.

# 🏆 Mini Challenge: print the full setup steps for a project.

def bootstrap(project_name):
    steps = [
        "python -m venv venv",
        "source venv/bin/activate",
        "pip install django",
        f"django-admin startproject {project_name}",
        f"cd {project_name}",
        "python manage.py runserver",
    ]
    for i, step in enumerate(steps, start=1):
        print(f"{i}. {step}")

bootstrap("portfolio")

# EXPECTED: a numbered 1-6 list ending with runserver.

❓ Frequently Asked Questions

Lesson complete — Django is installed and running!

You now know what Django is, why it's "batteries included," how the MTV pattern is organised, and how to install Django, create a project, and run the development server.

Practice quiz

What kind of framework is Django?

  • A JavaScript front-end framework
  • A high-level Python web framework
  • A CSS styling library
  • A database engine

Answer: A high-level Python web framework. Django is a high-level web framework written in Python.

What does 'batteries included' mean for Django?

  • It ships with an ORM, admin, templating, auth, and more out of the box
  • It requires no Python knowledge
  • It includes a physical battery
  • It only runs on mobile devices

Answer: It ships with an ORM, admin, templating, auth, and more out of the box. Django bundles most of what a web app needs so you can build immediately.

Which architectural pattern does Django follow?

  • MVVM
  • MVP
  • MTV (Model-Template-View)
  • Pub/Sub

Answer: MTV (Model-Template-View). Django uses MTV, its take on the classic MVC architecture.

In Django's MTV pattern, what does the Model represent?

  • HTML files with placeholders
  • Python classes that describe data and map to tables
  • The URL routing table
  • CSS stylesheets

Answer: Python classes that describe data and map to tables. Models are Python classes describing data that map to database tables.

Which command creates a new Django project's scaffolding?

  • python manage.py runserver
  • pip install django
  • django-admin startproject mysite
  • python -m django --version

Answer: django-admin startproject mysite. django-admin startproject generates the project scaffolding.

Which command starts Django's local development server?

  • python manage.py runserver
  • django-admin startapp
  • pip install django
  • python manage.py migrate

Answer: python manage.py runserver. manage.py runserver starts the development server.

How is Django typically installed?

  • By downloading a .exe
  • It comes preinstalled with Python
  • With npm install django
  • With pip install django

Answer: With pip install django. Django is installed via pip, Python's package manager.

Why install Django in a virtual environment?

  • It makes Django run faster
  • It keeps each project's dependencies isolated
  • It is required to use pip
  • It enables the admin site

Answer: It keeps each project's dependencies isolated. A virtual environment isolates each project's packages from others.

What is a Django view in MTV?

  • An HTML template
  • A database column
  • A function or class that takes a request and returns a response
  • A CSS file

Answer: A function or class that takes a request and returns a response. Views take an HTTP request and return a response.

Which file is the command-line utility for a Django project?

  • manage.py
  • settings.py
  • urls.py
  • wsgi.py

Answer: manage.py. manage.py is the command-line utility for project tasks.

Continue this course

Frequently asked questions

What is Django used for?

Django is a high-level Python web framework used to build database-driven websites and web applications quickly. It powers content sites, social networks, online stores, dashboards, and REST APIs. Companies like Instagram, Pinterest, and Mozilla have used Django because it handles the heavy lifting — routing, database access, templating, and security — so you can focus on your features.

Do I need to know Python before learning Django?

Yes, you should be comfortable with Python basics: variables, functions, classes, imports, and data structures like lists and dictionaries. Django is written in Python and you write your application logic in Python, so a solid grasp of the language makes the framework far easier to learn. You do not need to be an expert, but you should understand classes and functions.

What does 'batteries included' mean in Django?

It means Django ships with almost everything you need out of the box: an ORM for the database, an admin interface, a templating engine, form handling, authentication, security protections, and more. Unlike micro-frameworks where you assemble many third-party pieces, Django gives you a complete, integrated toolkit so you can build a real application immediately.

How do I create a new Django project?

After installing Django with 'pip install django', you run 'django-admin startproject mysite' to create the project scaffolding, then 'python manage.py runserver' to start the development server. Visiting http://127.0.0.1:8000/ shows Django's welcome page, confirming everything works.