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 blog app with forms and user accounts, and finally a newspaper app using a custom user model, email integration, foreign keys, authorization, permissions, and more.
By the end of this book you should 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 and used by millions of programmers every year. Its popularity is due to its friendliness to both beginners and advanced programmers: Django is robust enough to be 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 (2.1) and Python (3.7x). It also uses Pipenv which is now the officially recommended package manager by Python.org for managing Python packages and virtual environments. Throughout we’ll be using modern best practices from the Django, Python, and web development communities, especially 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:
- user authentication
- templates, routes, and views
- admin interface
- robust security
- support for multiple database backends
- and much much more
This approach makes our job as web developers much, much easier. We can focus on what makes our web application unique rather than reinventing the wheel when it comes to standard 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. And this 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. 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 recognize now 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 is that this book fills in the gaps and showcases how beginner-friendly Django really is.
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 learn Python, HTML, and CSS properly. 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 (2.1), Python (3.7), 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 application 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 Bitbucket.
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 require a minimal amount of code to use and extend core functionality in Django. They’re awesome as you’ll soon see. 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 Bitbucket, and deploy to Heroku.
Finally in Chapters 5-7 we’re ready for our final project: a robust blog application that demonstrates how to perform CRUD (Create-Read-Update-Delete) functionality in Django. We’ll find that Django’s generic class-based views mean we 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 (login, logout, signup).
In Chapter 8 we start a Newspaper site and introduce the concept of custom user models, a Django best practice that is rarely covered in tutorials. Simply put all new projects should use a custom user model and in this chapter you’ll learn how. Chapter 9 covers user authentication, Chapter 10 adds Bootstrap for styling, and Chapters 11-12 implement password reset and change via email. In 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.
The Conclusion provides an overview of the major concepts introduced in the book and a list of recommended resources for further learning.
While you can pick and choose chapters to read, the book’s structure is very deliberate. Each app/chapter introduces a new concept and reinforces past teachings. I highly recommend reading it 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 an 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
Complete source code for all examples can be found in the official Github repository.
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.