Beginning Django 3:
Build Full Stack
Python Web Applications
Greg Lim - Daniel Correa
Copyright 2021 Greg Lim
All rights reserved.
Copyright 2021 by Greg Lim
All rights reserved.
No part of this book may be reproduced in any form or by any electronic or mechanical means including information storage and retrieval systems, without permission in writing from the author. The only exception is by a reviewer, who may quote short excerpts in a review.
First Edition: November 2021
Co-Author: Daniel Correa
Table of Contents
Preface
About this book
In this book, we take you on a fun, hands-on and pragmatic journey to learning Django full stack development. You'll start building your first Django app within minutes. Every chapter is written in a bite-sized manner and straight to the point as we dont want to waste your time (and most certainly ours) on the content you don't need. In the end, you will have the skills to create a Movies review app and deploy it to the Internet.
In the course of this book, we will cover:
- Chapter 1: Introduction
- Chapter 2: Installing Python and Django
- Chapter 3: Understanding the Project Structure
- Chapter 4: Creating Our First App
- Chapter 5: URLs
- Chapter 6: Generating HTML Pages with Templates
- Chapter 7: Adding Bootstrap to Our Site
- Chapter 8: Adding a Search Form
- Chapter 9: Models
- Chapter 10: Django Admin Interface
- Chapter 11: Displaying Objects from Admin
- Chapter 12: Revisiting Concepts - Adding A News App
- Chapter 13: Understanding the Database
- Chapter 14: Displaying Objects in Admin
- Chapter 15: Extending Base Templates
- Chapter 16: Static Files
- Chapter 17: Movie Detail Page
- Chapter 18: Creating a Signup Form
- Chapter 19: Creating a User
- Chapter 20: Showing if a User is Logged In
- Chapter 21: Logout
- Chapter 22: Log In
- Chapter 23: Letting Users Post Movie Reviews
- Chapter 24: Creating a Review
- Chapter 25: Listing Reviews
- Chapter 26: Updating a Review
- Chapter 27: Deleting a Review
- Chapter 28: Authorization
- Chapter 29: Deployment
The goal of this book is to teach you Django development in a manageable way without overwhelming you. We focus only on the essentials and cover the material in a hands-on practice manner for you to code along.
Working Through This Book
This book is purposely broken down into short chapters where the development process of each chapter will center on different essential topics. The book takes a practical hands on approach to learning through practice. You learn best when you code along with the examples in the book.
Requirements
No previous knowledge on Django or Python is required, but you should have basic programming knowledge. We will explain concepts that are difficult to understand as we move along.
Getting Book Updates
To receive updated versions of the book, subscribe to our mailing list by sending a mail to . I try to update my books to use the latest version of software, libraries and will update the codes/content in this book. So, do subscribe to my list to receive updated copies!
Code Examples
While it is best that you type all the code by hand yourself, if you are stuck with a coding example or meet a strange error, you can obtain the source code of the completed project at www.greglim.co/p/django and check your code against it.
Chapter 1: Introduction
Welcome to Beginning Django 3! This book focuses on the key tasks and concepts to get you started to learn and build Django applications in a faster pace. It is designed for readers who do n t need all the details about Django at this point in the learning curve but concentrate on what you really need to know.
So wha t s Django? Django is a free, open source web framework for building modern Python web applications. Django helps you quickly build web apps by abstracting away many of the repetitive challenges in building a website, such as:
- connecting to a database,
- handling security
- user authentication
- URL routes,
- display content on a page through templates and forms
- supporting multiple database backends
- admin interface and more.
This allows developers to focus on building a web applicatio n s functionality rather than reinvent the wheel for standard web application functions.
Django is one of the most popular frameworks available and is used by established companies like Instagram, Pinterest, Mozilla, National Geographic and more. Yet, it is easy enough to be used for beginning startups and personal projects.
There are other popular frameworks like Flash in Python and Express in JavaScript (Greg has written a book about Express). But they provide the minimum required for a simple web page and developers have to do more foundational work, like installing and configuring third-party packages on their own for basic website functionality.
The App We Will Be Building
We will build a Movie reviews app which lets users view and search for movies. They can also log in and post reviews on the movies (fig. 1.1, 1.2, 1.3).
Figure 1.1 Home Page with search functionality
Figure 1.2 Movie page listing reviews
Figure 1.3 Create Review
Users can see the list of reviews in a Movi e s page and post/edit/delete their own review if they are logged in. They will not be able edit/delete othe r s reviews though. Through this app, we will learn a lot of concepts like forms, user authorization, permissions, foreign keys and more. By the end of this book, you will be confident creating your own Django projects.
We will begin by installing Python and Django in the next chapter.
Chapter 2: Installing Python and Django
Installing Python
Le t s check if we have Python installed and if so, what version is it.
If you are using a Mac, open your Terminal. If you are using Windows, open your Command Prompt. For convenience, I will address both the Terminal and Command Prompt as Termina l throughout the book.
We will need to check if we have at least Python 3.6 in order to use Django 3. To do so, go to your Terminal and run:
python3 --version
(or python on Windows)
This shows the version of Python you installed. Make sure that the version is at least 3.6. If it is not so, get the latest version of Python by going to python.org . Go to Download s and install the version for your OS.
After the installation, run python3 --version again and it should reflect the latest version of Python e.g. Python 3.9.7 (at time of boo k s writing)
Installing Django
We will be using pip to install Django. pip is the standard package manager for Python to install and manage packages not part of the standard Python library.
First check if you have pip installed by going to the Terminal and running: