Advertisement
⚡ Frameworks

Django Python Tutorial – Full-Stack Web Framework

Django is Python's most popular full-stack web framework — used by Instagram, Pinterest, Disqus, and Mozilla. It follows the MVT (Model-View-Template) pattern and comes "batteries included": authentication, admin panel, ORM, form validation, and security features out of the box.

⏱️ 35 min read🎯 Advanced📅 Updated 2026

Installing and Creating a Django Project

Install Django with pip and use django-admin to scaffold a project.

Python
# Install Django
# pip install django

# Create a new project
# django-admin startproject mysite
# cd mysite

# Create an app within the project
# python manage.py startapp blog

# Project structure:
# mysite/
#   manage.py       ← CLI tool
#   mysite/
#     settings.py   ← Configuration
#     urls.py       ← URL routing
#     wsgi.py       ← WSGI server entry
#   blog/
#     models.py     ← Database models
#     views.py      ← View functions
#     urls.py       ← App URL patterns

Models – Database Tables as Python Classes

Django models map directly to database tables. Django generates and runs SQL migrations automatically.

Python
# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    author = models.ForeignKey("auth.User", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    published = models.BooleanField(default=False)

    class Meta:
        ordering = ["-created_at"]

    def __str__(self):
        return self.title

# Run migrations:
# python manage.py makemigrations
# python manage.py migrate

Views – Handling HTTP Requests

Views receive HTTP requests and return responses.

Python
# blog/views.py
from django.shortcuts import render, get_object_or_404
from .models import Post

def post_list(request):
    posts = Post.objects.filter(published=True)
    return render(request, "blog/post_list.html", {"posts": posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk, published=True)
    return render(request, "blog/post_detail.html", {"post": post})
Advertisement

URL Routing

Map URLs to view functions using URLconf.

Python
# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("",           views.post_list,   name="post-list"),
    path("posts/<int:pk>/", views.post_detail, name="post-detail"),
]

# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("blog/",  include("blog.urls")),
]

The Django Admin Panel

Register models to get a fully-featured CRUD admin UI automatically.

Python
# blog/admin.py
from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ["title", "author", "published", "created_at"]
    list_filter  = ["published", "created_at"]
    search_fields = ["title", "content"]

# Create superuser:
# python manage.py createsuperuser
# Visit http://127.0.0.1:8000/admin/
💡
Tip

Django's admin is one of its killer features. You get a full CRUD interface for all your models with zero additional code.

Django: Batteries-Included Web Framework

Django's philosophy is "batteries included" — it ships with an ORM, admin panel, auth, forms, and security out of the box, so you build features instead of plumbing. It follows the MVT pattern (Model-View-Template).

PieceRole
Modela Python class → a database table (the ORM)
Viewa function/class handling a request
TemplateHTML with placeholders
URLconfmaps URLs → views
# models.py — define data; Django generates the table via migrations
class Post(models.Model):
    title = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)

# the ORM: query without writing SQL
Post.objects.filter(title__contains="python").order_by("-created")

The killer feature — the admin: register a model and Django auto-generates a full CRUD admin interface, saving weeks of internal-tooling work. Migrations: you change models in Python, run makemigrations + migrate, and Django evolves the database schema for you — version-controlled and reversible. When to choose Django: content-heavy sites, apps needing an admin and auth quickly, teams that value convention over configuration. For tiny APIs or maximum flexibility, a lighter framework (Flask/FastAPI) may fit better. Django + Django REST Framework is a common combo for APIs.

🏋️ Practical Exercise

Get started with Django:

  1. Install Django and create a project with django-admin startproject.
  2. Create an app and define a simple model with a couple of fields.
  3. Run makemigrations and migrate to create the database table.
  4. Register the model in the admin and add a record through the admin panel.

🔥 Challenge Exercise

Build a minimal blog app: a Post model (title, body, created date), a view that lists all posts, a URL route to reach it, and a template that renders them. Register Post in the Django admin and create a few entries there, then confirm they appear on your list page. Bonus: add a detail view with a route capturing the post id, and use the Django ORM to order posts by newest first.

📋 Summary

  • Django is a high-level, “batteries-included” web framework for building full applications quickly.
  • It follows the Model-Template-View (MTV) pattern.
  • The ORM maps Python model classes to database tables, so you rarely write raw SQL.
  • Migrations track and apply schema changes to the database.
  • The built-in admin panel gives a ready-made interface to manage your data.
  • A Django project is the overall site; apps are reusable components within it.

Interview Questions on Django

  • What is Django and what kind of applications is it for?
  • What is the MTV (Model-Template-View) architecture?
  • What is the Django ORM and what problem does it solve?
  • What are migrations and why are they needed?
  • What does the Django admin provide out of the box?
  • What is the difference between a project and an app in Django?
  • How does URL routing work in Django?

FAQ

What is the difference between Django and Flask? +

Django is full-featured and opinionated, bundling an ORM, admin, auth, and templating — great for large applications. Flask is a minimal micro-framework that gives you the basics and lets you choose your own components, ideal for small or highly custom apps.

What is the Django ORM? +

It is an object-relational mapper that lets you define database tables as Python classes (models) and query them with Python instead of SQL — for example Post.objects.filter(published=True).

Why do I need migrations? +

Migrations record changes to your models and apply them to the database schema in a controlled, versioned way, so your database structure stays in sync with your code across environments and teammates.

What is the difference between a project and an app? +

A project is the entire website and its configuration. An app is a self-contained module providing one piece of functionality (e.g. a blog or a store). One project can contain many apps, and apps can be reused across projects.