William S. Vincent
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
2018 - 2020 William S. Vincent
Introduction
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 worldInstagram, Pinterest, Bitbucket, Disqusbut 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 well be using modern best practices from the Django, Python, and web development communities including the thorough use of testing.
Why Django
A web framework is a collection of modular tools that abstracts away much of the difficultyand repetitioninherent in web development. For example, most websites need the same basic functionality: the ability to connect to a database, set URL routes, display content on a page, handle security properly, and so on. Rather than recreate all of this from scratch, programmers over the years have created web frameworks in all the major programming languages: Django and Flask in Python, Rails in Ruby, and Express in JavaScript among many, many others.
Django inherited Pythons batteries-included approach and includes out-of-the box support for common tasks in web development, including:
- user authentication
- testing
- 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.
In contrast, several popular frameworksmost notably Flask in Python and Express in JavaScriptadopt a microframework approach. They provide only the bare minimum required for a simple web page and leave it up to the developer to install and configure third-party packages to replicate basic website functionality. This approach provides more flexibility to the developer but also yields more opportunities for mistakes.
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 doesnt make sense to repeat the same codeand mistakeswhen 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 its written in the wonderfully readable yet still powerful Python programming language. In short, if youre 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 Djangos 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 sofrustrating! My goal with this book is to fill in the gaps and showcase how beginner-friendly Django really can be.
You dont 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.
Book Structure
We start by properly covering how to configure a local development environment in Chapter 1. Were 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, well 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 (Dont 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 well 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. Well 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.