Django for Professionals /

Introduction

Course Contents
  • Changelog
  • Introduction
  • Chapter 1: Initial Set Up
  • Chapter 2: Docker Hello, World!
  • Chapter 3: PostgreSQL
  • Chapter 4: Bookstore Project
  • Chapter 5: Pages App
  • Chapter 6: User Registration
  • Chapter 7: Static Assets
  • Chapter 8: Advanced User Registration
  • Chapter 9: Environment Variables
  • Chapter 10: Email
  • Chapter 11: Books App
  • Chapter 12: Reviews App
  • Chapter 13: File/Image Uploads
  • Chapter 14: Permissions
  • Chapter 15: Search
  • Chapter 16: Performance
  • Chapter 17: Security
  • Chapter 18: Deployment
  • Conclusion

Welcome to Django for Professionals, a guide to building professional websites with 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 course will show you how to bridge that gap on Windows and macOS computers.

When installing Django and creating 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 using SQLite as the default database, a local web server, local static asset hosting, a built-in User model, DEBUG mode turned on, and many other settings implicitly configured.

Many, if not most, of these settings must be reconfigured for a production project. 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 course 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 new to Django or web development, this is not the course 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 recommend starting with my course Django for Beginners. It begins with the basics and progressively introduces concepts by building five increasingly complex Django applications. After completing that book, you will be ready for success with this book.

I have also written a course 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 helpful to your education as a Django developer but optional before reading this book.

We will use Docker throughout most of this course 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 be used extensively in this book.

Course 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 introduces 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 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 and includes robust tests. In Chapter 6, a complete user registration flow is implemented from scratch using the built-in auth app for signup, login, and logout. 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, including email-only loin 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 course is intentional. When it comes time to build your own Django projects, 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 book's second half focuses on specific features related to our Bookstore website.

Chapter 11 starts with building 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, including the addition of django-debug-toolbar to inspect 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 and 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.

Course 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, the previous content is unchanged in the function below 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

The complete source code for the course can be found in the official Github repository. It is a good place to check first for 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, 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 community members, 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, meet other developers, and enhance your 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.