Welcome to Django for Beginners, a project-based approach to learning web development with the Django web framework. In this book you will build five progressively more complex web applications, starting with a simple Hello, World app, progressing to a Pages app, a Message Board app, a Blog app with forms and user accounts, and finally a Newspaper app that uses a custom user model, email integration, foreign keys, authorization, permissions, and more. By the end of this book you will feel confident creating your own Django projects from scratch using current best practices.
Django is a free, open source web framework written in the Python programming language. A “web framework” is software that abstracts away many of the common challenges related to building a website, such as connecting to a database, handling security, user accounts, and so on. These days most developers rely on web frameworks rather than trying to build a website truly from scratch. Django in particular was first released in 2005 and has been in continuous development since then. Today, it is one of the most popular web frameworks available, used by the largest websites in the world–Instagram, Pinterest, Bitbucket, Disqus–but also flexible enough to be a good choice for early-stage startups and prototyping personal projects.
This book is regularly updated and features the latest versions of both Django and Python. It also uses Pipenv for managing Python packages and virtual environments, though using Pip works fine as well. Throughout we’ll be using modern best practices from the Django, Python, and web development communities including the thorough use of testing.
Django inherited Python’s “batteries-included” approach and includes out-of-the box support for common tasks in web development, including:
- user authentication
- database models, forms, URL routes, and templates
- admin interface
- security and performance upgrades
- support for multiple database backends
This approach allows web developers to focus on what makes a web application unique rather than reinventing the wheel every time for standard, secure web application functionality.
As of 2019 Django has been under active development for over 14 years which makes it a grizzled veteran in software years. Millions of programmers have already used Django to build their websites, which is undeniably a good thing. Web development is hard. It doesn’t make sense to repeat the same code–and mistakes–when a large community of brilliant developers has already solved these problems for us.
At the same time, Django remains under active development and has a yearly release schedule. The Django community is constantly adding new features and security improvements. And best of all it’s written in the wonderfully readable yet still powerful Python programming language. In short, if you’re building a website from scratch Django is a fantastic choice.
Why This Book
I wrote this book because while Django is extremely well documented there is a severe lack of beginner-friendly tutorials available. When I first learned Django years ago, I struggled to even complete the official polls tutorial. Why was this so hard I remember thinking?
With more experience, I now recognize that the writers of the Django docs faced a difficult choice: they could emphasize Django’s ease-of-use or its depth, but not both. They choose the latter and as a professional developer I appreciate the choice, but as a beginner I found it so…frustrating! My goal with this book is to fill in the gaps and showcase how beginner-friendly Django really can be.
You don’t need previous Python or web development experience to complete this book. It is intentionally written so that even a total beginner can follow along and feel the magic of writing their own web applications from scratch. However if you are serious about a career in web development, you will eventually need to invest the time to properly learn Python, HTML, and CSS. A list of recommended resources for further study is included in the Conclusion.
We start by properly covering how to configure a local development environment in Chapter 1. We’re using bleeding edge tools in this book: the most recent version of Django (3.1), Python (3.8), and Pipenv to manage our virtual environments. We also introduce the command line and discuss how to work with a modern text editor.
In Chapter 2 we build our first project, a minimal Hello, World app that demonstrates how to set up new Django projects. Because establishing good software practices is important, we’ll also save our work with Git and upload a copy to a remote code repository on GitHub.
In Chapter 3 we make, test, and deploy a Pages app that introduces templates and class-based views. Templates are how Django allows for DRY (Don’t Repeat Yourself) development with HTML and CSS while class-based views are quite powerful yet require a minimal amount of code. We also add our first tests and deploy to Heroku, which has a free tier we’ll use throughout this book. Using platform-as-a-service providers like Heroku transforms development from a painful, time-consuming process into something that takes just a few mouse clicks.
In Chapter 4 we build our first database-backed project, a Message Board app. Django provides a powerful ORM that allows us to write concise Python for our database tables. We’ll explore the built-in admin app which provides a graphical way to interact with our data and can be even used as a Content Management System (CMS) similar to Wordpress. Of course, we also write tests for all our code, store a remote copy on GitHub, and deploy to Heroku.
In Chapters 5-7 we’re ready for our next project: a robust Blog app that implements CRUD (Create-Read-Update-Delete) functionality. By using Django’s generic class-based views we only have to write only a small amount of actual code for this. Then we’ll add forms and integrate Django’s built-in user authentication system for sign up, log in, and log out functionality.
The remainder of the book, Chapters 8-16, is dedicated to building a robust Newspaper site, starting with the introduction to custom user models in Chapter 8, a Django best practice that is rarely addressed in tutorials. Chapter 9 covers user authentication, Chapter 10 adds Bootstrap for styling, and Chapters 11-12 implement password reset and change via email. With Chapters 13-15 we add articles and comments to our project, along with proper permissions and authorizations. We even learn some tricks for customizing the admin to display our growing data. And in Chapter 16, environment variables are introduced alongside proper deployment techniques.
The Conclusion provides an overview of the major concepts introduced in the book and a list of recommended resources for further learning.
While you could pick and choose chapters to read, the book’s structure is deliberate. Each app/chapter introduces a new concept and reinforces past teachings. I highly recommend reading the book in order, even if you’re eager to skip ahead. Later chapters won’t cover previous material in the same depth as earlier chapters.
By the end of this book you’ll have a solid understanding of how Django works, the ability to build apps on your own, and the background needed to fully take advantage of additional resources for learning intermediate and advanced Django techniques.
There are many code examples in this book, which are denoted as follows:
# This is Python code print(Hello, World)
For brevity we will use dots
... to denote existing code that remains unchanged, for example, in a function we are updating.
def make_my_website: ... print("All done!")
We will also use the command line console frequently (starting in Chapter 1: Initial Setup) to execute commands, which take the form of a
$ prefix in traditional Unix style.
$ echo "hello, world"
The result of this particular command is the next line will state:
We will typically combine a command and its output. The command will always be prefaced by a
$ and the output will not. For example, the command and result above will be represented as follows:
$ echo "hello, world" hello, world
Official Source Code
Complete source code for all chapters can be found in the official GitHub repository. While it’s best to type all the code by hand yourself, if you do find yourself stuck with a coding example or seeing a strange error, make sure to check your code against the official repo. And if you’re still stuck, try copy and pasting the official source code. A common error is subtle white spacing differences that are almost impossible to detect to the naked eye.
Django is an excellent choice for any developer who wants to build modern, robust web applications with a minimal amount of code. It is popular, under active development, and thoroughly battle-tested by the largest websites in the world. In the next chapter we’ll learn how to configure any computer for Django development.
Continue on to Chapter 1: Initial Set Up.