Introduction to the Updated Edition
Django has continued to change since the release of the first edition of Mastering Django two years ago. Django now has a new Long Term Support (LTS) version with Django 3.2, and, as I write this in mid-December 2021, Django 4.0 has just been released.
This new edition updates the code and text for Django 3.2. As the differences between Django 3.2 and Django 4 are minor, I will issue a free supplemental text for those of you wanting to go straight to Django 4 in your coding.
There is no new content in this edition. It is always a trade off between what to include in a textbook like this, and what can be effectively managed, given how quickly software changes. The publishing cycle is time consuming and laborious and I have decided to keep the book content as-is, but add expanded material and tutorials to the djangobook.com website. This allows you to get updated content while keeping the project manageable for me.
Introducing Mastering Django
When Django 2 was released, a lot changed in the Django world.
Not just because we finally got rid of the complications with having to deal with both Python 2 and 3, but also because of the many new features, tweaks, updates and optimizations that ensure Django keeps getting better and better. Django 3 continues the tradition of continual improvement in Django’s codebase.
What many of you may not know, is the original Mastering Django:Core was an update of the original book written by two of the creators of Django—Adrian Holovaty and Jacob Kaplan-Moss. Given the original book had been around since Django 1.1, it got dated. There are also a lot of similarities to the Django docs in several chapters.
(Funny aside: I have had a couple of people email me and take me to task for copying the docs. Lol! Given that the guys who wrote the original book also wrote the original docs, that will happen folks!)
I have decided to start with a clean sheet of paper for Mastering Django. This means you don’t just get an update; you get a new book, written from scratch to meet the needs of today’s programmers.
First and foremost, the book remains a plain-English, easy to follow deep-dive into Django’s commonly used core functionalities. It covers both Django 2 and Django 3.
Second, the book complements the existing docs; it doesn’t just reproduce them in a different format. I have removed all the original material from Jacob and Adrian’s book and all the content from the Django documentation. There are lots of topics not covered adequately in the docs, which provide ample opportunities for me to create resources that will make you a better Django programmer. I have done my best to include them in this book.
Third, the book takes a more practical approach, with less emphasis on theory and more exploration of working code. It’s impossible to put all Django’s functions into a single book, but I have done my best to include the functions, classes and tools you will use regularly. With each major function or class, I expand them into functioning code examples. Source code is also available for Mastering Django.
And finally, while I will not be releasing the book as open-source, the early chapters will remain free to access and read on djangobook.com. As with the previous book, all income from sales support the Django Book project, allowing me to keep the core content ad-free and accessible to all.
Exciting times ahead! ????
All the best with your programming journey!
Who This Book is For
This book is a programmer’s manual targeted at intermediate to advanced programmers wishing to gain an in-depth understanding of Django.
In saying that, it doesn’t mean beginners can’t get value out of the book. Since the publication of the first edition of the book, roughly half of the readers I have spoken to identified as being a beginner when they started out with the book.
The way I write—building on simple concepts and explaining every step—is highly accessible to beginners, so if you are a beginner, you will still learn a great deal about Django from the book.
Where the book can challenge beginners is all the peripheral stuff—HTML, CSS, Python and web development in general—that I don’t explain in any detail. The book is big enough as it is, without me trying to teach you all the stuff you need to know that isn’t Django-related!
I do, however, give you lots of references so you can easily get more information if you need it.
Structure of the Book
I’ve broken the book up into two parts:
- PART 1: Fundamentals—A high-level overview of Django, how it’s structured, and its core components, so you can grasp how Django brings together each element to create powerful and scalable web applications.
- PART 2: Essentials—This is the meaty part of the book where we take a deep and detailed dive into the core modules of Django, so you can gain a thorough and practical understanding of all the most commonly used modules in Django.
Throughout the book, I will use snippets of code from a fictitious website for a social or sporting club called MyClub.
It was my original plan for this book to end up with a complete website for MyClub. However, it was apparent halfway through that a book that teaches you as much as possible about the popular parts of Django has very different goals to a book that teaches you how to create a complex, professional website.
In the latter case, no more so than the need to write tests. Tests, while necessary for developing a professional application, distract from learning the core functions of Django. If I wrote proper tests for all the code in this book, I would double the size of the codebase and add little to your learning. For these reasons, I have kept the code in this book to illustrative snippets.
Required Programming Knowledge
The book assumes you have little to no experience with Django.
I expect you to have a basic understanding of web technologies like HTML and CSS, and a basic understanding of how to structure code. You should also be familiar with your OS’s terminal or shell program. Absolute beginners shouldn’t be too concerned, as it’s easy to learn the basics, and that’s all you need to get value out of the book.
As Django is written in Python, I also assume you have a basic understanding of Python. Although, since Python is so easy to learn and there are such great resources available online for Python, I haven’t heard from a learner yet who said not knowing Python was a barrier. You will learn a lot about Python just by learning Django, and all the extras you need to know are easy to find online.
As the book only covers core Django, you don’t require any special functions or libraries, so the latest versions of Python 3.9 and Django 3 are OK. At the time of writing, this is Python 3.9.8 and Django 3.2.8.
I provide installation instructions for Windows and Mac users. Linux users can refer to the 90 million Linux installation tutorials online (OK, so that’s maybe an exaggeration, but there are lots).
All code in this book will run on Windows, macOS or Linux. While the code and screenshots are all from a Windows machine, the fundamentals remain the same—all three have a terminal or command window, and management commands like
runserver work the same on all three platforms.
Coding style is also identical across platforms with one exception—I use Windows-style backslashes in file paths. This is to assist Windows users to differentiate between Windows’ native use of backslashes and Django’s implementation of forward slashes in path names. Linux and macOS users, simply need to substitute forward slashes in these cases.
All browser screenshots are taken from the Chrome browser. If you are using FireFox, Safari or some other browser, your screen may look different than the screenshots.
The images are in full-color in the PDF and eBook versions, but are grayscale in the printed book (color is way too expensive to print). Paperback users are encouraged to run the code to see the full-color effect in your browser.
Free Django 4 Supplement
The differences between Django 3 and Django 4 are minor. Rather than release a new book for Django 4, I am going to make Django 4 updates available as a free supplement to this book.
Once the first supplement becomes available, you will be able to download it from: https://masteringdjango.com/booksupplements. I will publish any future update for newer versions of Django 4 at the same URL.
You can download the source code and resources in this book from
The included source has been written in Django 3.2. As there is little difference between Django 3.0 and 3.2, I have only updated the relevant files that changed for Django 3.2. This means that some documentation references in the source still point to Django 3.0 docs. This will not affect the operation of the code. The source has been tested against the version of SQLite that comes with Django. While the data structures used in the book are simple and shouldn’t cause problems if you decide to use another database, there are no guarantees. If you find any database-related quirks, refer to the database engine documentation.
The source is broken up into folders, one for each chapter of the book. Rather than delete code that changes from chapter to chapter, I have commented out lines of code in the source so you can see where the code has changed.
Code line numbering in the book is provided so you can easily cross-reference my explanations to individual lines of code in the book. In most cases, line numbering in the book does not match line numbers in the source files.
The source is not designed to be executed as is. The SQLite database file and migrations for each chapter have been removed from the source. While copying the source code from a chapter and running it inside a virtual environment will work in most cases (after running the migrations), there is no guarantee it will. The source code is for your reference and to assist your learning, it’s not fully functioning code that you can just copy and use in your projects.
I will be publishing errata (bugs and typos) information and updates for future versions of Django on the website here:
Please check the errata page before reporting any bug or typo you have found in the book.
Found a Bug or Typo?
With a book project the size of Mastering Django the odd bug or typo will slip through. This is even after countless edits from me and plenty of feedback from my awesome beta testers (you know who you are ????).
In a perfect world, I’d be able to pay the megabucks for professional editors, but there’s a reason why those of us who support Django all have day jobs; it ain’t exactly a meal ticket.
So, if you find a bug or a typo, get in touch with me via the Djangobook website and let me know so I can fix the error. The major plus of self-publishing is that I can make changes quickly and send updates to everyone as soon as I have a batch of edits completed.
For any other questions regarding the book, you can contact me via the help page on the Djangobook website. Note that, due to working full-time, it’s very difficult for me to find time to answer Django questions that don’t directly relate to my books and courses.
You first points of contact for general information on Django and answers to “how do I…” questions should be Stack Overflow, your favorite search engine, and any one of the dozens of social media groups relating to Django.