Django 4 By Example
Fourth Edition
Build powerful and reliable Python web applications from scratch
Django 4 By Example
Fourth Edition
Copyright © 2022 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Senior Publishing Product Manager: Manish Nainani
Acquisition Editor – Peer Reviews: Suresh Jain
Project Editor: Amisha Vathare
Content Development Editor: Bhavesh Amin
Copy Editor: Safis Editing
Technical Editor: Aditya Sawant
Proofreader: Safis Editing
Indexer: Sejal Dsilva
Presentation Designer: Pranit Padwal
First published: November 2015
Second edition: May 2018
Third edition: March 2020
Fourth edition: August 2022
Production reference: 2230822
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80181-305-1
To my sister Paloma.
Django: The web framework for perfectionists with deadlines.
I like this tagline because it can be easy for developers to fall prey to perfectionism when having to deliver workable code on time.
There are many great web frameworks out there, but sometimes they assume too much of the developer, for example, how to properly structure a project, find the right plugins and elegantly use existing abstractions.
Django takes most of that decision fatigue away and provides you with so much more. But it's also a big framework, so learning it from scratch can be overwhelming.
I learned Django in 2017, head-on, out of necessity, when we decided it would be our core technology for our Python coding platform (CodeChalleng.es). I forced myself to learn the ins and outs by building a major real-world solution that has served thousands of aspiring and experienced Python developers since its inception.
Somewhere in this journey, I picked up an early edition of this book. It turned out to be a treasure trove. Very close to our hearts at Pybites, it teaches you Django by building interesting, real-world applications. Not only that, Antonio brings a lot of real-world experience and knowledge to the table, which shows in how he implements those projects.
And Antonio never misses an opportunity to introduce lesser-known features, for example, optimizing database queries with Postgres, useful packages like django-taggit, social auth using various platforms, (model) managers, inclusion template tags, and much more.
In this new edition, he even added additional schemas, images, and notes in several chapters and moved from jQuery to vanilla JavaScript (nice!)
This book not only covers Django thoroughly, using clean code examples that are well explained, it also explains related technologies which are a must for any Django developer: Django REST Framework, django-debug-toolbar, frontend / JS, and, last but not least, Docker.
More importantly, you'll find many nuances that you'll encounter and best practices you'll need to be an effective Django developer in a professional setting.
Finding a multifaceted resource like this is hard, and I want to thank Antonio for all the hard work he consistently puts into keeping it up to date.
As a Python developer that uses Django a lot, Django by Example has become my GO TO guide, an unmissable resource I want to have close by my desk. Every time I come back to this book, I learn new things, even after having read it multiple times and having used Django for a solid five years now.
If you embark on this journey, be prepared to get your hands dirty. It's a practical guide, so brew yourself a good coffee and expect to sink your teeth into a lot of Django code! But that's how we best learn, right? :)
- Bob Belderbos
Co-Founder of Pybites
is the co-founder and chief technology officer of Nucoro, the fintech platform that allows financial institutions to build, automate, and scale digital wealth management products. Antonio is also CTO of Exo Investing, an AI-driven digital investment platform for the UK market.
Antonio has been developing Django projects since 2006 for clients across several industries. In 2009 Antonio founded Zenx IT, a development company specialized in building digital products. He has been working as a CTO and technology consultant for multiple technology-based startups and he has managed development teams building projects for large digital businesses. Antonio holds an MSc. in Computer Science from ICAI - Universidad Pontificia Comillas, where he mentors early-stage startups. His father inspired his passion for computers and programming.
is a software craftsman from Bangladesh. He has a decade-long professional experience working with Python and Django. Besides working for different start-ups and clients, Asif also contributes to some frequently used Python and Django packages. For his open-source contributions, he is now a core maintainer of Celery, oAuthLib, PyJWT, and auditwheel. He is also co-maintainer of several Django and Django REST framework extension packages. He is a voting member of the Django Software Foundation (DSF) and a contributing/managing member of the Python Software Foundation (PSF). He has been mentoring many young people to learn Python and Django, both professionally and personally.
A special thanks to Karen Stingel and Ismir Kullolli for reading and providing feedback on the book to enhance the content further. Your help is much appreciated!