Welcome to Django for Professionals, a guide to building professional websites with the Django, a Python-based web framework. There is a massive gulf between building simple “toy apps” that can be created and deployed quickly and what it takes to build a “production-ready” web application suitable for deployment to thousands or even millions of users. This book will show you to how to bridge that gap on both Windows and macOS computers.

When you first install Django and create a new project the default settings are geared towards local development. And this makes sense: there’s no need to add all the additional features required of a large website until you know you need them. These defaults include the use of SQLite as the default database, a local web server, local static asset hosting, built-in User model, DEBUG mode turned on, and many other settings that are implicitly set.

For a production project many, if not most, of these settings must be reconfigured. And even then there can be a frustrating lack of agreement among the experts. Rather than overwhelm the reader with the full array of choices available this book shows one approach for building a professional website grounded in current Django community best practices. The topics covered include using Docker for local development and deployment, PostgreSQL, a custom user model, robust user authentication flow with email, comprehensive testing, environment variables, security and performance improvements, and more.

By the end of this book you will have built a professional website step-by-step and learned about additional areas for further exploration. Whether you are starting a new project that hopes to be as large as Instagram (currently the largest Django website in the world) or making much-needed updates to an existing Django project, you will have the tools and knowledge to do so.

Prerequisites

If you’re brand-new to either Django or web development, this is not the book for you. The pace will be far too fast. While you could read along, copy all the code, and have a working website at the end, I instead recommend starting with my book Django for Beginners. It starts with the very basics and progressively introduces concepts via building five increasingly complex Django applications. After completing that book you will be ready for success with this book.

I have also written a book on transforming Django websites into web APIs called Django for APIs. In practice most Django developers work in teams with other developers and focus on back-end APIs, not full-stack web applications that require dedicated JavaScript front-ends. Reading Django for APIs is therefore helpful to your education as a Django developer, but not required before reading this book.

We will use Docker throughout most of this book but still rely, briefly, on having Python and Django installed locally. Git and the command line are also necessary components of the modern developers toolchain and will both be used extensively in this book as well.

Book Structure

Chapter 1 starts with setting up your local computer for development by using the command line, installing Python, configuring Git, and creating virtual environments. Chapter 2 is an introduction to Docker and explores how to “Dockerize” a traditional Django project. In Chapter 3 PostgreSQL is introduced, a production-ready database that we can run locally within our Docker environment and also deploy to production. Then Chapter 4 starts the main project in the book: an online Bookstore featuring a custom user model, search, image uploads, permissions, and a host of other goodies.

Chapter 5 focuses on building out a Pages app for a basic homepage along with robust testing which is included with every new feature on the site. In Chapter 6 a complete user registration flow is implemented from scratch using the built-in auth app for sign up, log in, and log out. Chapter 7 introduces proper static asset configuration for CSS, JavaScript, and images as well as the addition of Bootstrap for styling.

In Chapter 8 the focus shifts to advanced user registration, namely including email-only log in and social authentication via the third-party django-allauth package. Chapter 9 introduces environment variables, a key component of Twelve-Factor App development and a best practice widely used in the web development community. Rounding out the set up of our project, Chapter 10 focuses on email and adding a dedicated third-party provider.

The structure of the first half of the book is intentional. When it comes time to build your own Django projects, chances are you will be repeating many of the same steps from Chapters 3-9. After all, every new project needs proper configuration, user authentication, and environment variables. So treat these chapters as your detailed explanation and guide. The second half of the book focuses on specific features related to our Bookstore website.

Chapter 11 starts with building out the models, tests, and pages for our Bookstore via a Books app. There is also a discussion of URLs and switching from id to a slug to a UUID (Universally Unique IDentifier) in the URLs. Chapter 12 features the addition of reviews to our Bookstore and a discussion of foreign keys.

In Chapter 13 image-uploading is added and in Chapter 14 permissions are set across the site to lock it down. For any site but especially e-commerce, search is a vital component and Chapter 15 walks through building a form and increasingly complex search filters for the site.

In Chapter 16 the focus switches to performance optimizations, inspecting queries and templates, database indexes, front-end assets, and multiple built-in caching options. Chapter 17 covers security in Django, both the built-in options as well as additional configurations that can–and should–be added for a production environment. The final section, Chapter 18, is on deployment, the standard upgrades needed to migrate away from the Django web server, local static file handling, and configuring ALLOWED_HOSTS.

The Conclusion touches upon various next steps to take with the project and additional Django best practices.

Book Layout

There are many code examples in this book, which are formatted as follows:

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

For brevity we will use three dots, ..., to denote existing code that remains unchanged in a longer code example. For example, in the function below, the previous content is unchanged and the print() statement has been added. In these cases there will also be a comment, # new, indicating where the new code has been added.

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

Complete source code for the book can be found in the official Github repository. It is a good place to check first if there are any issues with your code.

Community

“Come for the framework, stay for the community” is a common saying among Django developers. While the Django code base is very impressive, ultimately the health of the project up to this point and going forward depends upon the community that has grown around it. Django development happens publicly on the django-developers list and the project is overseen by a non-profit, the Django Software Foundation, which manages contributions, supports annual DjangoCon conferences, and local meetups where developers gather to share knowledge and insights. There is an official Django forum populated by many members of the community which is an ideal place to ask for help.

No matter what your level of technical expertise becoming involved in Django itself is a great way to learn, to meet other developers, and to enhance your own reputation.

Conclusion

Django is an excellent choice for any developer who wants to build modern, robust web applications with a minimal amount of code. In the next chapter we’ll make sure your computer is properly set up for web development before diving deeply into Django itself in the rest of the book.