Django for Beginners /

Introduction

Course Contents

Welcome to Django for Beginners, a project-based approach to learning web development with Django, a free and open-source web framework written in Python. Django is used by everyone, from students and startup founders to the world's largest websites, including Instagram, Pinterest, Disqus, Mozilla, and NASA. Its "batteries-included" approach provides all the built-in functionality you need to create powerful, real-world web applications quickly, hence its tagline, "The web framework for perfectionists with deadlines."

Django's abundance of features can feel overwhelming to newcomers. It doesn't help that the official polls tutorial and Django documentation target intermediate-to-advanced web developers, not beginners.

The good news is that, as a "loosely coupled" framework, Django's components work independently or together, allowing for a high degree of modularity. In other words, you only have to use (and learn) what you need. Even professional developers with years of experience will use only some of what the framework offers; it's simply too big and expansive for all its features to fit into a single project.

You will find, though, that the same patterns and tasks arise in almost every Django website: create and structure a new project, connect to and query a database, add logic, perform CRUD (Create-Read-Update-Delete) operations, handle user accounts and forms, and so on. Django doesn't have to feel overwhelming; it shouldn't! There is a built-in solution for almost every conceivable use case: that's what the documentation is for! But no one, even the original creators and core developers who wrote much of the documentation, can keep it all in their heads. You shouldn't attempt to either!

This book started as my personal notes on building Django projects. It took a long time before I had internalized and felt comfortable with Django's structure. The best way to solidify my understanding was to create progressively more complex projects focused on a new concept or skill. Eventually, I published my notes as a series of blog posts and, based on their popularity, created this book, now in its sixth edition.

In this book, you will learn how to build, test, and optimize six progressively more complex web applications. We will start with a Hello, World application and conclude with a real-world Newspaper website that ties together all the fundamental concepts and techniques covered in the book, including models, views, URLs, templates, forms, user accounts, permissions, and more. A final chapter adds a web API to that same site with Django REST Framework, so its articles can be consumed by a mobile app or a JavaScript frontend as well as a browser—an increasingly common requirement in modern web development. By the end of this book, you should feel confident creating Django projects from scratch and have the background to fill in any knowledge gaps with more advanced educational resources.

Why Learn Django?

Django was initially created in the fall of 2003 at the Lawrence Journal-World newspaper and named after the famous jazz guitarist Django Reinhardt; it was released as a free, open-source project in July 2005. That makes it over twenty years old now, quite mature in software terms, but it has continued to thrive and is arguably more vibrant today than ever before. The framework accepts double-digit code submissions each week, releases security and bugfix updates monthly, and ships a new feature release roughly every eight months (moving to once a year in 2028, as we'll see below). A vast ecosystem of third-party packages provides additional functionality beyond the core framework.

Django is written in Python, a wonderfully readable yet powerful programming language and arguably the most popular in the world today. Python is the default choice in most undergraduate computer science curricula, the dominant language for data science and artificial intelligence, and widely used in scientific research. Its ease of use and broad applicability make Python suitable for almost any task.

Django inherited Python's "batteries-included" approach and includes a wide range of built-in features for routine tasks in web development, including:

  • ORM (Object-Relational Mapper): write Python rather than raw SQL for creating and querying database tables
  • Authentication: a full-featured and secure system for user accounts, groups, permissions, and cookie-based user sessions
  • Templating Engine: a simple syntax for adding variables and logic to create dynamic HTML
  • Forms: a powerful form library that handles rendering and validation
  • URL Routing: a clean, elegant URL schema that is easy to maintain and reason about
  • Admin Interface: a visual way to interact with all website data, including users and database tables
  • Internationalization: multilingual support plus locale-specific formatting of dates, time, numbers, and time zones
  • Security: protection against SQL injection, cross-site scripting, cross-site request forgery, clickjacking, and remote code execution

Millions of programmers have used Django to build websites, and millions more adopt it each year, building on the work of a large community of developers who have already solved these problems.

Django vs FastAPI vs Flask

The Python web framework space is dominated by three options: Django, FastAPI, and Flask. Django is the oldest of the three, and it was traditionally a two-horse race between Flask and Django, but in recent years FastAPI has grown in popularity. So what are the differences?

Flask, released in 2010, is the original Python microframework—minimalist and most commonly used for small server-rendered websites or APIs. FastAPI (2018) takes a different approach: async-first and API-first, built around serving JSON to Single-Page Apps (SPAs), mobile clients, and machine learning backends.

What Flask and FastAPI share is a microframework philosophy: they provide the bare minimum scaffolding and rely on third-party packages for everything else. This makes them lightweight and flexible, but it also means assembling and maintaining a stack of separately authored packages for authentication, the ORM, forms, templating, and admin tools—code Django ships by default. Each third-party package is one more thing to evaluate, integrate, secure, and keep up to date.

No single framework is best; it depends on what you're building. This book focuses on Django, so the bias naturally favors Django's choices. As you grow as a developer, exploring Flask and FastAPI on your own will deepen your understanding of the trade-offs web frameworks make.

Prerequisites

You don't need previous web development experience to complete this book, though it is helpful if you have a basic familiarity with HTML, CSS, and a small amount of JavaScript. All three are inescapable parts of modern web development. Python is a wonderfully beginner-friendly and readable programming language, which makes it a popular first language for many newcomers. You don't need to be a Python expert, but you will benefit from an understanding of Python basics such as variables, object-oriented programming, and basic syntax. Many free and paid resources are worth exploring before tackling this book if you are a true beginner. Eric Matthes's classic book, Python Crash Course, is a wonderful guide and even includes a later chapter on Django.

Book Structure

Chapter 1 begins by showing you how to set up a local development environment for Windows and macOS. We then learn about the powerful command line, Git and GitHub, configuring text editors, and installing the latest versions of Python and Django.

In Chapter 2, we review how websites and web frameworks work before diving into an overview of Django architecture. From there, we build our first project, a minimal Hello, World website, while learning about views, URLs, and apps. We even save our work with Git for the first time.

In Chapter 3, we make and test a Personal Website that introduces function-based views, templates, and the Django Template Language. We explore the template context, write our first tests using Django's built-in testing framework, and push our code to a remote GitHub repository for the first time.

Class-based views, template inheritance, and more advanced testing patterns are covered in Chapter 4, where we build a Company Website. This is the final project before we turn to Django models and database-backed websites.

In Chapter 5, we build our first database-backed project: a Message Board website. Django provides a powerful ORM (Object-Relational Mapper) that abstracts away the need to write raw SQL ourselves. Instead, we can write Python in a models.py file that the ORM automatically translates into the correct SQL for multiple database backends (PostgreSQL, MySQL, SQLite, MariaDB, and Oracle). We'll explore the built-in admin app, which provides a graphical way to interact with data. Of course, we also write tests for all our code and store a remote copy on GitHub.

In Chapters 6-8, we're ready for a Blog website that implements CRUD (Create-Read-Update-Delete) functionality. First, we'll use function-based views and then switch to Django's generic class-based views, so we only have to write a small amount of actual code. Then, we'll add forms and integrate Django's built-in user authentication system for signup, login, and logout functionality.

The remainder of the book is dedicated to building a production-ready Newspaper website. Chapter 9 shows how to set up a new project with a custom user model and appropriate tests. Chapter 10 covers a complete user authentication flow of login, logout, and signup, while Chapter 11 adds Bootstrap for enhanced CSS styling. Chapter 12 implements password change and password reset via email. Chapters 13-15 add articles, comments, proper permissions, and authorizations. Chapter 16 turns to performance: we install django-debug-toolbar, find the redundant database queries hiding in the pages we have already built, and fix them. Chapter 17 covers production-ready deployment. Finally, Chapter 18 is an optional introduction to web APIs: we add one to the Newspaper site with Django REST Framework so the same articles can be consumed by a mobile app or a JavaScript frontend.

The Conclusion lists recommended resources for further learning. While it may be tempting to skip around in this book, I recommend reading the chapters in order. Each chapter introduces a new concept and builds upon past teachings. By the end of this book, you'll have a solid understanding of Django, the ability to build your own apps, and the background required to fully take advantage of additional resources for learning intermediate and advanced Django techniques.

What's New in Django 6.1

Django 6.1 was released in August 2026 and requires Python 3.12, 3.13, or 3.14. Django's versioning policy is time-based rather than feature-based: releases go out on a published schedule with whatever features are ready at the time. Django has such a large and active community of contributors that the community decided years ago to focus on regular rollouts rather than waiting for specific features to be completed.

That schedule is changing, and since you will be reading Django version numbers for years to come, the switch is worth understanding. Historically, feature releases were numbered A.B—4.2, 5.0, 5.1, 6.0, 6.1—and arrived roughly every eight months. Django 6.2, due in April 2027, is the last release numbered that way. Beginning with the release in January 2028, Django adopts calendar versioning: each feature release is named for its year and ships every January. So the release after 6.2 is simply Django 2028, followed by Django 2029 the January after that, and so on. Patch releases work as they always have, just with the new prefix: 6.1.2 today, 2028.3 a couple of years from now.

The support policy is changing alongside the numbering. Under the old policy, certain releases—those ending in .2, like Django 5.2 and 6.2—were designated long-term support (LTS) releases and received security and data loss fixes for a guaranteed three years, while other releases were supported for a shorter window. That distinction existed largely for bigger companies that struggled to keep up with Django's release pace. Starting with Django 2028, every feature release receives the same three-year support period, so the LTS label is no longer needed. My advice is unchanged and now easier to follow: run the latest release you can.

So, what's new in Django 6.1? The headline change for our purposes is a new MAILERS setting that replaces the older collection of EMAIL_* settings for configuring email; we will use it in Chapter 12 when adding password reset. Elsewhere, model fields gained configurable "fetch modes" that control what happens when code touches data that wasn't loaded, which we mention briefly in Chapter 16; ForeignKey can now hand deletions off to the database itself, and the admin received a round of accessibility improvements to its forms.

Django is a mature web framework that strives to remain stable yet advance alongside the modern web. If you find yourself on a project with an older version of Django, there are detailed instructions for updating to the latest version.

Book Layout

There are many code examples in this book styled as follows:

# This is Python code
print("Hello, World!")

For brevity, we will use three dots, ..., when the existing code has not changed. The section of code that has changed is highlighted using a # new comment.

def make_my_website():
    ...
    print("All done!")  # new

How to Use AI

There has been a seismic shift in programming over the last few years due to the rise of AI and, more specifically, Large Language Models (LLMs) such as ChatGPT, Claude, and others. Python and Django, in particular, work exceptionally well with AI given their longevity, documentation, and popularity; there is ample training data for LLMs to consume and train on. As a result, if you are trying to write code with AI, you'll get better results with Python and Django than with almost any other programming language and web framework.

If anything, understanding programming is more valuable than ever because AI models can act as a great accelerator if you already know the fundamentals. They are great at spotting typos, suggesting approaches, and sometimes writing code. They can spot bugs, recommend updates, write tests, and so on. Used responsibly and under a human's watchful eye, AI models can do amazing things.

However, current hype promising that newer models, agentic workflows, and the rest will replace human developers is overblown. I have been using LLMs for three years now, and they are improving dramatically. Even a year ago, most prompts generated boilerplate code that was only sometimes passable. Today, with the right prompts, context, and a frontier-level model, you can output code that could pass for the work of many developers. But to get the most out of AI and to use it well, you still have to know what you're doing. To be an effective human-in-the-loop, it is not enough to write prompts without understanding; you need your own knowledge of programming, an understanding of architectures, trade-offs, use cases, and all the rest.

If you do reach an impasse while working through this book, I encourage you not to immediately jump to AI for help. Look at the official source code on GitHub, and try to spot the errors yourself. Maybe ask AI to spot it if you are well and truly stuck, but don't start with that approach.

Also remember that it's okay to feel confused by new concepts; in fact, this is what learning feels like. Embrace it! The timeless techniques of stepping away from the computer screen when stuck, taking a walk, or, even better, sleeping on a problem are still good advice. It's incredible what a small amount of rest and distance will do to your mind when solving problems.

Community

There is a saying among long-time Django developers, "Come for the framework, stay for the community." It's true. Django's success owes as much to its community as to the framework's technical achievements.

That community takes many forms. Django is not governed by a single company or individual, but by the non-profit Django Software Foundation (DSF), whose mission is to promote, support, and advance the framework. The foundation's members elect its directors, who currently serve two-year terms—I served on the board as Treasurer from 2020 to 2022. In person, the community organizes annual, volunteer-run DjangoCon conferences in the US, Europe, and Africa, along with local meetups in cities worldwide. Online, the Django Forum and the official Discord are active, welcoming places to ask questions and learn, whatever your level of experience.

Getting involved is one of the best ways to grow as a developer. Whether you answer a question on the forum, attend a local meetup, or contribute code at a DjangoCon sprint, you'll learn faster, meet other developers, and discover that Django is unusually warm to beginners and veterans alike.

Conclusion

In the next chapter, you'll learn how to properly set up your computer and create your first Django project. Let's begin!