35,99 €
If you want to learn Django by doing, this book is for you.
Django 5 By Example is the fifth edition of the best-selling franchise that helps you build real-world web apps. This book will walk you through planning and creation, solving common problems, and implementing best practices using a step-by-step approach.
You’ll cover a wide range of web application development topics through four different projects: a blog application, a social website, an e-commerce application, and an e-learning platform. Pick up what’s new in Django 5 as you build end-to-end Python web apps, follow detailed project plans, and understand the hows and whys of Django.
This is a practical and approachable book that will have you creating web apps quickly.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 922
Veröffentlichungsjahr: 2024
Django 5 By Example
Fifth Edition
Build powerful and reliable Python web applications from scratch
Antonio Melé
Django 5 By Example
Fifth Editon
Copyright © 2024 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.
Publishing Product Manager: Lucy Wan
Acquisition Editor – Peer Reviews: Jane Dsouza
Senior Project Editor: Rianna Rodrigues
Senior Development Editor: Matthew Davies
Development Editor: Shazeen Iqbal
Copy Editor: Safis Editing
Technical Editor: Kushal Sharma
Proofreader: Safis Editing
Indexer: Rekha Nair
Presentation Designer: Rajesh Shirsath
Senior Developer Relations Marketing Executive: Priyadarshini Sharma
First published: November 2015
Second edition: May 2018
Third edition: March 2020
Fourth edition: August 2022
Fifth edition: April 2024
Production reference: 1250424
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK.
ISBN 978-1-80512-545-7
www.packt.com
To my mother, Lola.
“Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.”
This is the description of Django that you find on its website and in its Git repository, and I think it defines it in a concise but complete way.
In the almost 20 years since the first Django release, there have been many new web frameworks that have arisen and grown rapidly, based on Python and other languages, occupying their specific niches and exploiting new technologies.
Despite everything, Django has continued its growth. It has improved its functionality, ensured its stability, and innovated in an iterative but inexorable way, version after version, all the way up to its latest release.
I started using Django from version 1.3, after having used Zope and Plone, and I was immediately impressed by how much it helped me build real-world applications in a fast yet effective way. Over the years, the characteristics of the web applications I have created have changed, but Django has always remained a valid tool for responding to technical challenges.
Over the years, I have tried writing down my experiences of the Django features that I appreciate most, trying to demonstrate them with examples. When I discovered this book, I immediately saw myself in Antonio’s approach, which supplements your discovery of Django with concrete applications that you create along the way.
In addition to the many commonly used Django features, I appreciate how this book presents useful packages, such as Django REST framework and the Django Debug Toolbar, and that there are also examples of using more advanced features, such as full-text search with PostgreSQL (which I’m very fond of) and cache integration with Redis.
Furthermore, in this new edition, you will find many of the new features introduced in Django 5.0, such as database-computed default values, admin facets, and simplified templates for form field rendering.
The projects from the old edition have also been revisited with Python 3.12, updated third-party packages, and updated setup instructions. I found nice additions like explanatory diagrams and advanced DRF functionality.
When I was young, I started learning HTML, trying to edit web pages I visited with a text editor, and I have remained fond of a hands-on approach to learning technologies. I like getting my hands dirty before studying a technology and I like doing it by trying to create something concrete. I recommend this approach to you, too.
This book will take you on a similar journey, in which you will alternate between the study of Django functionalities and their use in concrete examples. Get ready to get your hands dirty!
– Paolo Melchiorre
Python developer / Django contributor
www.paulox.net
Antonio Melé serves as Engineering Director at Backbase, a fintech leader in digital transformation for financial institutions. He joined Backbase in 2023, following its acquisition of Nucoro, a digital wealth management platform that he co-founded.
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 Universidad Pontificia Comillas and completed the Advanced Management Program at MIT Sloan. His father inspired his passion for computers and coding.
I couldn’t have done this without an incredible group of Python enthusiasts and experts who took the time to dive into the early versions of this book. A huge thanks to everyone who reviewed the book and read the beta drafts. Your insights, corrections, and enthusiasm really brought this project to life.
Mark Walker has previously developed e-learning software in the government and defense sectors. Beginning Django development with a niche offline application, he moved on to develop projects for the world’s biggest running events. He then branched out into DevOps, load testing, and securing applications for penetration testing. In recent years, he has become the technical lead for the Django CMS Association and a Django Software Foundation member. He has also worked for fantasy sports games, shifting from AWS to GCP. Over the years Mark has become a maintainer for several packages and a navigator of the Djangonaut space program, which aims to mentor developers who want to become contributors in the Django ecosystem.
The following list comprises readers from our beta program who kindly guided the development of this edition through their feedback. We would like to thank the following individuals for their valuable assistance reviewing this edition:
Adewunmi OladeleAshkan Ranjbar BeyranvandAshraf Ibrahim MohamedAttila SebőkChedjou Soffo RocelinEduardo Andrés Muñoz MuñozEmmanuel AidooFlorian BautryGladys García GranadilloHenry UkomahIsmir KullolliKaren StingelMelissa StrongMohammad Ahmad Mohammad AbdelSalamMoustapha DiarraOleg BelovRasa LazovicSafwan Samsudeen Sergio Delgado QuinteroTakahiro KatoYardley Luther Estiverne EstiverneRead this book alongside other users, Django development experts, and the author himself. Ask questions, provide solutions to other readers, chat with the author via Ask Me Anything sessions, and much more.Scan the QR code or visit the link to join the community.
https://packt.link/Django5ByExample
Preface
Who this book is for
What this book covers
To get the most out of this book
Share your thoughts
Download a free PDF copy of this book
Building a Blog Application
Installing Python
Creating a Python virtual environment
Installing Django
Installing Django with pip
Django overview
Main framework components
The Django architecture
New features in Django 5
Creating your first project
Applying initial database migrations
Running the development server
Project settings
Projects and applications
Creating an application
Creating the blog data models
Creating the Post model
Adding datetime fields
Defining a default sort order
Adding a database index
Activating the application
Adding a status field
Adding a many-to-one relationship
Creating and applying migrations
Creating an administration site for models
Creating a superuser
The Django administration site
Adding models to the administration site
Customizing how models are displayed
Adding facet counts to filters
Working with QuerySets and managers
Creating objects
Updating objects
Retrieving objects
Filtering objects
Using field lookups
Chaining filters
Excluding objects
Ordering objects
Limiting QuerySets
Counting objects
Checking if an object exists
Deleting objects
Complex lookups with Q objects
When QuerySets are evaluated
More on QuerySets
Creating model managers
Building list and detail views
Creating list and detail views
Using the get_object_or_404 shortcut
Adding URL patterns for your views
Creating templates for your views
Creating a base template
Creating the post list template
Accessing our application
Creating the post detail template
The request/response cycle
Management commands used in this chapter
Summary
Additional resources
Join us on Discord!
Enhancing Your Blog and Adding Social Features
Functional overview
Using canonical URLs for models
Creating SEO-friendly URLs for posts
Modifying the URL patterns
Modifying the views
Modifying the canonical URL for posts
Adding pagination
Adding pagination to the post list view
Creating a pagination template
Handling pagination errors
Building class-based views
Why use class-based views
Using a class-based view to list posts
Recommending posts by email
Creating forms with Django
Handling forms in views
Sending emails with Django
Working with environment variables
Sending emails in views
Rendering forms in templates
Creating a comment system
Creating a model for comments
Adding comments to the administration site
Creating forms from models
Handling ModelForms in views
Creating templates for the comment form
Adding comments to the post detail view
Adding comments to the post detail template
Using simplified templates for form rendering
Summary
Additional resources
Extending Your Blog Application
Functional overview
Implementing tagging with django-taggit
Retrieving posts by similarity
Creating custom template tags and filters
Implementing custom template tags
Creating a simple template tag
Creating an inclusion template tag
Creating a template tag that returns a QuerySet
Implementing custom template filters
Creating a template filter to support Markdown syntax
Adding a sitemap to the site
Creating feeds for blog posts
Adding full-text search to the blog
Installing Docker
Installing PostgreSQL
Dumping the existing data
Switching the database in the project
Loading the data into the new database
Simple search lookups
Searching against multiple fields
Building a search view
Stemming and ranking results
Stemming and removing stop words in different languages
Weighting queries
Searching with trigram similarity
Summary
Expanding your project using AI
Additional resources
Building a Social Website
Functional overview
Creating a social website project
Starting the social website project
Using the Django authentication framework
Creating a login view
Using Django’s built-in authentication views
Login and logout views
Change password views
Reset password views
User registration and user profiles
User registration
Extending the user model
Installing Pillow and serving media files
Creating migrations for the profile model
Using a custom user model
Summary
Additional resources
Join us on Discord!
Implementing Social Authentication
Functional overview
Technical requirements
Using the messages framework
Building a custom authentication backend
Preventing users from using an existing email address
Adding social authentication to your site
Running the development server through HTTPS
Authentication using Google
Creating a profile for users that register with social authentication
Summary
Additional resources
Sharing Content on Your Website
Functional overview
Creating an image bookmarking website
Building the image model
Creating many-to-many relationships
Registering the image model in the administration site
Posting content from other websites
Cleaning form fields
Installing the Requests library
Overriding the save() method of a ModelForm
Building a bookmarklet with JavaScript
Creating a detail view for images
Creating image thumbnails using easy-thumbnails
Adding asynchronous actions with JavaScript
Loading JavaScript on the DOM
Cross-site request forgery for HTTP requests in JavaScript
Performing HTTP requests with JavaScript
Adding infinite scroll pagination to the image list
Summary
Additional resources
Tracking User Actions
Functional overview
Building a follow system
Creating many-to-many relationships with an intermediate model
Creating list and detail views for user profiles
Adding user follow/unfollow actions with JavaScript
Creating an activity stream application
Using the contenttypes framework
Adding generic relations to your models
Avoiding duplicate actions in the activity stream
Adding user actions to the activity stream
Displaying the activity stream
Optimizing QuerySets that involve related objects
Using select_related()
Using prefetch_related()
Creating templates for actions
Using signals for denormalizing counts
Working with signals
Application configuration classes
Using Django Debug Toolbar
Installing Django Debug Toolbar
Django Debug Toolbar panels
Django Debug Toolbar commands
Counting image views with Redis
Installing Redis
Using Redis with Python
Storing image views in Redis
Storing a ranking in Redis
Next steps with Redis
Summary
Expanding your project using AI
Additional resources
Building an Online Shop
Functional overview
Creating an online shop project
Creating product catalog models
Registering catalog models on the administration site
Building catalog views
Creating catalog templates
Building a shopping cart
Using Django sessions
Session settings
Session expiration
Storing shopping carts in sessions
Creating shopping cart views
Adding items to the cart
Building a template to display the cart
Adding products to the cart
Updating product quantities in the cart
Creating a context processor for the current cart
Context processors
Setting the cart in the request context
Registering customer orders
Creating order models
Including order models in the administration site
Creating customer orders
Creating asynchronous tasks
Working with asynchronous tasks
Workers, message queues, and message brokers
Using Django with Celery and RabbitMQ
Monitoring Celery with Flower
Summary
Additional resources
Join us on Discord!
Managing Payments and Orders
Functional overview
Integrating a payment gateway
Creating a Stripe account
Installing the Stripe Python library
Adding Stripe to your project
Building the payment process
Integrating Stripe Checkout
Testing the checkout process
Using test credit cards
Checking the payment information in the Stripe dashboard
Using webhooks to receive payment notifications
Creating a webhook endpoint
Testing webhook notifications
Referencing Stripe payments in orders
Going live
Exporting orders to CSV files
Adding custom actions to the administration site
Extending the administration site with custom views
Generating PDF invoices dynamically
Installing WeasyPrint
Creating a PDF template
Rendering PDF files
Sending PDF files by email
Summary
Additional resources
Extending Your Shop
Functional overview
Creating a coupon system
Building the coupon model
Applying a coupon to the shopping cart
Applying coupons to orders
Creating coupons for Stripe Checkout
Adding coupons to orders on the administration site and to PDF invoices
Building a recommendation engine
Recommending products based on previous purchases
Summary
Additional resources
Adding Internationalization to Your Shop
Functional overview
Internationalization with Django
Internationalization and localization settings
Internationalization management commands
Installing the gettext toolkit
How to add translations to a Django project
How Django determines the current language
Preparing your project for internationalization
Translating Python code
Standard translations
Lazy translations
Translations including variables
Plural forms in translations
Translating your own code
Translating templates
The {% translate %} template tag
The {% blocktranslate %} template tag
Translating the shop templates
Using the Rosetta translation interface
Fuzzy translations
URL patterns for internationalization
Adding a language prefix to URL patterns
Translating URL patterns
Allowing users to switch language
Translating models with django-parler
Installing django-parler
Translating model fields
Integrating translations into the administration site
Creating migrations for model translations
Using translations in QuerySets
Adapting views for translations
Format localization
Using django-localflavor to validate form fields
Expanding your project using AI
Summary
Additional resources
Building an E-Learning Platform
Functional overview
Setting up the e-learning project
Serving media files
Building the course models
Registering the models in the administration site
Using fixtures to provide initial data for models
Creating models for polymorphic content
Using model inheritance
Abstract models
Multi-table model inheritance
Proxy models
Creating the Content models
Creating custom model fields
Adding ordering to Module and Content objects
Adding authentication views
Adding an authentication system
Creating the authentication templates
Summary
Additional resources
Join us on Discord!
Creating a Content Management System
Functional overview
Creating a CMS
Creating class-based views
Using mixins for class-based views
Working with groups and permissions
Restricting access to class-based views
Managing course modules and their contents
Using formsets for course modules
Adding content to course modules
Managing modules and their contents
Reordering modules and their contents
Using mixins from django-braces
Summary
Additional resources
Rendering and Caching Content
Functional overview
Displaying the catalog of courses
Adding student registration
Creating a student registration view
Enrolling in courses
Rendering course contents
Accessing course contents
Rendering different types of content
Using the cache framework
Available cache backends
Installing Memcached
Installing the Memcached Docker image
Installing the Memcached Python binding
Django cache settings
Adding Memcached to your project
Cache levels
Using the low-level cache API
Checking cache requests with Django Debug Toolbar
Low-level caching based on dynamic data
Caching template fragments
Caching views
Using the per-site cache
Using the Redis cache backend
Monitoring Redis with Django Redisboard
Summary
Additional resources
Building an API
Functional overview
Building a RESTful API
Installing Django REST framework
Defining serializers
Understanding parsers and renderers
Building list and detail views
Consuming the API
Extending serializers
Adding additional fields to serializers
Implementing serializer method fields
Adding pagination to views
Building the course serializer
Serializing relations
Creating nested serializers
Creating ViewSets and routers
Building custom API views
Handling authentication
Implementing basic authentication
Adding permissions to views
Adding additional actions to ViewSets
Creating custom permissions
Serializing course contents
Consuming the RESTful API
Summary
Additional resources
Building a Chat Server
Functional overview
Creating a chat application
Implementing the chat room view
Real-time Django with Channels
Asynchronous applications using ASGI
The request/response cycle using Channels
Installing Channels and Daphne
Writing a consumer
Routing
Implementing the WebSocket client
Enabling a channel layer
Channels and groups
Setting up a channel layer with Redis
Updating the consumer to broadcast messages
Adding context to the messages
Modifying the consumer to be fully asynchronous
Persisting messages into the database
Creating a model for chat messages
Adding the message model to the administration site
Storing messages in the database
Displaying the chat history
Integrating the chat application with existing views
Summary
Additional resources
Going Live
Creating a production environment
Managing settings for multiple environments
Local environment settings
Running the local environment
Production environment settings
Using Docker Compose
Installing Docker Compose via Docker Desktop
Creating a Dockerfile
Adding the Python requirements
Creating a Docker Compose file
Configuring the PostgreSQL service
Applying database migrations and creating a superuser
Configuring the Redis service
Serving Django through WSGI and NGINX
Using uWSGI
Configuring uWSGI
Using NGINX
Configuring NGINX
Using a hostname
Serving static and media assets
Collecting static files
Serving static files with NGINX
Securing your site with SSL/TLS
Checking your project for production
Configuring your Django project for SSL/TLS
Creating an SSL/TLS certificate
Configuring NGINX to use SSL/TLS
Redirecting HTTP traffic over to HTTPS
Configuring Daphne for Django Channels
Using secure connections for WebSockets
Including Daphne in the NGINX configuration
Creating a custom middleware
Creating subdomain middleware
Serving multiple subdomains with NGINX
Implementing custom management commands
Summary
Expanding your project using AI
Additional resources
Join us on Discord!
Other Books You May Enjoy
Index
Cover
Index
Once you’ve read Django 5 By Example, Fifth Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily.
Follow these simple steps to get the benefits:
Scan the QR code or visit the link below:https://packt.link/free-ebook/9781805125457
Submit your proof of purchase.That’s it! We’ll send your free PDF and other benefits to your email directly.In this book, you will learn how to build professional-grade web projects using Django. This initial chapter will guide you through the essential building blocks of a Django application, from installation to deployment. If you haven’t set up Django on your machine yet, the Installing Django section will walk you through the installation process.
Before starting our first Django project, let’s go over what you are about to learn. This chapter will give you a general overview of the framework. It will guide you through the different major components to create a fully functional web application: models, templates, views, and URLs. You will gain an understanding of how Django works and how the different framework components interact.
You will also learn the difference between Django projects and applications, and you will learn about the most important Django settings. You will build a simple blog application that allows users to navigate through all published posts and read individual posts. You will also create a simple administration interface to manage and publish posts. In the next two chapters, you will extend the blog application with more advanced functionalities.
Consider this chapter as your roadmap for constructing a fully-fledged Django application. Don’t be concerned if some components or concepts appear unclear at first. The different framework components will be explored in detail throughout this book.
This chapter will cover the following topics:
Installing PythonCreating a Python virtual environmentInstalling DjangoCreating and configuring a Django projectBuilding a Django applicationDesigning data modelsCreating and applying model migrationsSetting up an administration site for your modelsWorking with QuerySets and model managersBuilding views, templates, and URLsUnderstanding the Django request/response cycleYou will start by installing Python on your machine.
The source code for this chapter can be found at https://github.com/PacktPublishing/Django-5-by-example/tree/main/Chapter01.
All Python packages used in this chapter are included in the requirements.txt file in the source code for the chapter. You can follow the instructions to install each Python package in the following sections, or you can install all the requirements at once with the command python -m pip install -r requirements.txt.
Django 5.0 supports Python 3.10, 3.11, and 3.12. In the examples in this book, we will use Python 3.12.
If you’re using Linux or macOS, you probably have Python installed. If you’re using Windows, you can download a Python installer from the python.org website. You can download Python for your OS from https://www.python.org/downloads/.
Open the command-line shell prompt of your machine. If you are using macOS, press Command + spacebar to open Spotlight and write Terminal to open Terminal.app. If you are using Windows, open the Start menu and type powers into the search box. Then, click on the Windows PowerShell application to open it. Alternatively, you can use the more basic command prompt by typing cmd into the search box and clicking on the Command Prompt application to open it.
Verify that Python 3 is installed on your machine by typing the following command in the shell prompt:
python3 --versionIf you see the following, then Python 3 is installed on your computer:
Python 3.12.3If you get an error, try the python command instead of python3. If you use Windows, it’s recommended that you replace python with the py command.
If your installed Python version is lower than 3.12, or if Python is not installed on your computer, download Python 3.12 from https://www.python.org/downloads/ and follow the instructions to install it. On the download site, you can find Python installers for Windows, macOS, and Linux.
Throughout this book, when Python is referenced in the shell prompt, we will use the python command, though some systems may require using python3. If you are using Linux or macOS and your system’s Python is Python 2, you will need to use python3 to use the Python 3 version you installed. Note that Python 2 reached end-of-life in January 2020 and shouldn’t be used anymore.
In Windows, python is the Python executable of your default Python installation, whereas py is the Python launcher. The Python launcher for Windows was introduced in Python 3.3. It detects what Python versions are installed on your machine and it automatically delegates to the latest version.
If you use Windows, you should use the py command. You can read more about the Windows Python launcher at https://docs.python.org/3/using/windows.html#launcher.
Next, you are going to create a Python environment for your project and install the necessary Python libraries.
When you write Python applications, you will usually use packages and modules that are not included in the standard Python library. You may have Python applications that require a different version of the same module. However, only a specific version of a module can be installed system-wide. If you upgrade a module version for an application, you might end up breaking other applications that require an older version of that module.
To address this issue, you can use Python virtual environments. With virtual environments, you can install Python modules in an isolated location rather than installing them system-wide. Each virtual environment has its own Python binary and can have its own independent set of installed Python packages in its site-packages directory.
Since version 3.3, Python comes with the venv library, which provides support for creating lightweight virtual environments. By using the Python venv module to create isolated Python environments, you can use different package versions for different projects. Another advantage of using venv is that you won’t need any administrative privileges to install Python packages.
If you are using Linux or macOS, create an isolated environment with the following command:
python -m venv my_envRemember to use python3 instead of python if your system comes with Python 2 and you installed Python 3.
If you are using Windows, use the following command instead:
py -m venv my_envThis will use the Python launcher in Windows.
The previous command will create a Python environment in a new directory named my_env. Any Python libraries you install while your virtual environment is active will go into the my_env/lib/python3.12/site-packages directory.
If you are using Linux or macOS, run the following command to activate your virtual environment:
source my_env/bin/activateIf you are using Windows, use the following command instead:
.\my_env\Scripts\activateThe shell prompt will include the name of the active virtual environment enclosed in parentheses, like this:
(my_env) zenx@pc:~ zenx$You can deactivate your environment at any time with the deactivate command. You can find more information about venv at https://docs.python.org/3/library/venv.html.
If you have already installed Django 5.0, you can skip this section and jump directly to the Creating your first project section.
Django comes as a Python module and thus can be installed in any Python environment. If you haven’t installed Django yet, the following is a quick guide to installing it on your machine.
The pip package management system is the preferred method of installing Django. Python 3.12 comes with pip preinstalled, but you can find pip installation instructions at https://pip.pypa.io/en/stable/installation/.
Run the following command at the shell prompt to install Django with pip:
python -m pip install Django~=5.0.4This will install Django’s latest 5.0 version in the Python site-packages directory of your virtual environment.
Now we will check whether Django has been successfully installed. Run the following command in a shell prompt:
python -m django --versionIf you get an output that starts with 5.0, Django has been successfully installed on your machine. If you get the message No module named Django, Django is not installed on your machine. If you have issues installing Django, you can review the different installation options described at https://docs.djangoproject.com/en/5.0/intro/install/.
All Python packages used in this chapter are included in the requirements.txt file in the source code for the chapter, mentioned above. You can follow the instructions to install each Python package in the following sections, or you can install all requirements at once with the command pip install -r requirements.txt.
Django is a framework consisting of a set of components that solve common web development problems. Django components are loosely coupled, which means they can be managed independently. This helps separate the responsibilities of the different layers of the framework; the database layer knows nothing about how the data is displayed, the template system knows nothing about web requests, and so on.
Django offers maximum code reusability by following the DRY (don’t repeat yourself) principle. Django also fosters rapid development and allows you to use less code by taking advantage of Python’s dynamic capabilities, such as introspection.
You can read more about Django’s design philosophies at https://docs.djangoproject.com/en/5.0/misc/design-philosophies/.
Django follows the MTV (Model-Template-View) pattern. It is a slightly similar pattern to the well-known MVC (Model-View-Controller) pattern, where the template acts as the view and the framework itself acts as the controller.
The responsibilities in the Django MTV pattern are divided as follows:
Model: This defines the logical data structure and is the data handler between the database and the view.Template: This is the presentation layer. Django uses a plain-text template system that keeps everything that the browser renders.View: This communicates with the database via the model and transfers the data to the template for viewing.The framework itself acts as the controller. It sends a request to the appropriate view, according to the Django URL configuration.
When developing any Django project, you will always work with models, views, templates, and URLs. In this chapter, you will learn how they fit together.
Figure 1.1 shows how Django processes requests and how the request/response cycle is managed with the different main Django components – URLs, views, models, and templates:
Figure 1.1: The Django architecture
This is how Django handles HTTP requests and generates responses:
A web browser requests a page by its URL and the web server passes the HTTP request to Django.Django runs through its configured URL patterns and stops at the first one that matches the requested URL.Django executes the view that corresponds to the matched URL pattern.The view potentially uses data models to retrieve information from the database.Data models provide data definitions and behaviors. They are used to query the database.The view renders a template (usually HTML) to display the data and returns it with an HTTP response.We will get back to the Django request/response cycle at the end of this chapter in the The request/response cycle section.
Django also includes hooks in the request/response process, which are called middleware. Middleware has been intentionally left out of this diagram for the sake of simplicity. You will use middleware in different examples of this book, and you will learn how to create custom middleware in Chapter 17, Going Live.
We have covered the foundational elements of Django and how it processes requests. Let’s explore the new features introduced in Django 5.
Django 5 introduces several key features that you will use in the examples of this book. This version also deprecates certain features and eliminates previously deprecated functionalities. Django 5.0 presents the following new major features:
Facet filters in the administration site: Facet filters can be added now to the administration site. When enabled, facet counts are displayed for applied filters in the admin object list. This feature is presented in the Added facet counts to filters section of this chapter.Simplified templates for form field rendering: Form field rendering has been simplified with the capability to define field groups with associated templates. This aims to make the process of rendering related elements of a Django form field, such as labels, widgets, help text, and errors, more streamlined. An example of using field groups can be found in the Creating templates for the comment form section of Chapter 2, Enhancing Your Blog and Adding Social Features.Database-computed default values: Django adds database-computed default values. An example of this feature is presented in the Adding datetime fields section of this chapter.Database-generated model fields: This is a new type of field that enables you to create database-generated columns. An expression is used to automatically set the field value each time the model is changed. The field value is set using the GENERATEDALWAYS SQL syntax.More options for declaring model field choices: Fields that support choices no longer require accessing the .choices attribute to access enumeration types. A mapping or callable instead of an iterable can be used directly to expand enumeration types. Choices with enumeration types in this book have been updated to reflect these changes. An instance of this can be found in the Adding a status field section of this chapter.Django 5 also comes with some improvements in asynchronous support. Asynchronous Server Gateway Interface (ASGI) support was first introduced in Django 3 and improved in Django 4.1 with asynchronous handlers for class-based views and an asynchronous ORM interface. Django 5 adds asynchronous functions to the authentication framework, provides support for asynchronous signal dispatching, and adds asynchronous support to multiple built-in decorators.
Django 5.0 drops support for Python 3.8 and 3.9.
You can read the complete list of changes in the Django 5.0 release notes at https://docs.djangoproject.com/en/5.0/releases/5.0/.
As a time-based release, there are no drastic changes in Django 5, making it straightforward to upgrade Django 4 applications to the 5.0 release.
If you want to quickly upgrade an existing Django project to the 5.0 release, you can use the django-upgrade tool. This package rewrites the files of your project by applying fixers up to a target version. You can find instructions to use django-upgrade at https://github.com/adamchainz/django-upgrade.
The django-upgrade tool is inspired by the pyupgrade package. You can use pyupgrade to automatically upgrade syntax for newer versions of Python. You can find more information about pyupgrade at https://github.com/asottile/pyupgrade.
Your first Django project will consist of a blog application. This will offer you a solid introduction to Django’s capabilities and functionalities.
Blogging is the perfect starting point to build a complete Django project, given its wide range of required features, from basic content management to advanced functionalities like commenting, post sharing, search, and post recommendations. The blog project will be covered in the first three chapters of this book.
In this chapter, we will start by creating the Django project and a Django application for the blog. We will then create our data models and synchronize them to the database. Finally, we will create an administration site for the blog, and we will build the views, templates, and URLs.
Figure 1.2 shows a representation of the blog application pages that you will create:
Figure 1.2: Diagram of functionalities built in Chapter 1
The blog application will consist of a list of posts including the post title, publishing date, author, a post excerpt, and a link to read the post. The post list page will be implemented with the post_list view. You will learn how to create views in this chapter.
When readers click on the link of a post in the post list page, they will be redirected to a single (detail) view of a post. The detail view will display the title, publishing date, author, and the complete post body.
Let’s start by creating the Django project for our blog. Django provides a command that allows you to create an initial project file structure.
Run the following command in your shell prompt:
django-admin startproject mysiteThis will create a Django project with the name mysite.
Avoid naming projects after built-in Python or Django modules in order to prevent conflicts.
Let’s take a look at the generated project structure:
mysite/ manage.py mysite/ __init__.py asgi.py settings.py urls.py wsgi.pyThe outer mysite/ directory is the container for our project. It contains the following files:
manage.py: This is a command-line utility used to interact with your project. You won’t usually need to edit this file.mysite/: This is the Python package for your project, which consists of the following files:__init__.py: An empty file that tells Python to treat the mysite directory as a Python module.asgi.py: This is the configuration to run your project as an ASGI application with ASGI-compatible web servers. ASGI is the emerging Python standard for asynchronous web servers and applications.settings.py: This indicates settings and configuration for your project and contains initial default settings.urls.py: This is the place where your URL patterns live. Each URL defined here is mapped to a view.wsgi.py: This is the configuration to run your project as a Web Server Gateway Interface (WSGI) application with WSGI-compatible web servers.Django applications require a database to store data. The settings.py file contains the database configuration for your project in the DATABASES setting. The default configuration is a SQLite3 database. SQLite comes bundled with Python 3 and can be used in any of your Python applications. SQLite is a lightweight database that you can use with Django for development. If you plan to deploy your application in a production environment, you should use a full-featured database, such as PostgreSQL, MySQL, or Oracle. You can find more information about how to get your database running with Django at https://docs.djangoproject.com/en/5.0/topics/install/#database-installation.
Your settings.py file also includes a list named INSTALLED_APPS that contains common Django applications that are added to your project by default. We will go through these applications later in the Project settings section.
Django applications contain data models that are mapped to database tables. You will create your own models in the Creating the blog data models section. To complete the project setup, you need to create the tables associated with the models of the default Django applications included in the INSTALLED_APPS setting. Django comes with a system that helps you manage database migrations.
Open the shell prompt and run the following commands:
cd mysite python manage.py migrateYou will see an output that ends with the following lines:
Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OKThe preceding lines are the database migrations that are applied by Django. By applying the initial migrations, the tables for the applications listed in the INSTALLED_APPS setting are created in the database.
You will learn more about the migrate management command in the Creating and applying migrations section of this chapter.
Django comes with a lightweight web server to run your code quickly, without needing to spend time configuring a production server. When you run the Django development server, it keeps checking for changes in your code. It reloads automatically, freeing you from manually reloading it after code changes. However, it might not notice some actions, such as adding new files to your project, so you will have to restart the server manually in these cases.
Start the development server by typing the following command in the shell prompt:
python manage.py runserverYou should see something like this:
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). January 01, 2024 - 10:00:00 Django version 5.0, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.Now, open http://127.0.0.1:8000/ in your browser. You should see a page stating that the project is successfully running, as shown in Figure 1.3:
Figure 1.3: The default page of the Django development server
The preceding screenshot indicates that Django is running. If you take a look at your console, you will see the GET request performed by your browser:
[01/Jan/2024 10:00:15] "GET / HTTP/1.1" 200 16351Each HTTP request is logged in the console by the development server. Any error that occurs while running the development server will also appear in the console.
You can run the Django development server on a custom host and port or tell Django to load a specific settings file, as follows:
python manage.py runserver 127.0.0.1:8001 --settings=mysite.settingsWhen you have to deal with multiple environments that require different configurations, you can create a different settings file for each environment.
This server is only intended for development and is not suitable for production use. To deploy Django in a production environment, you should run it as a WSGI application using a web server, such as Apache, Gunicorn, or uWSGI, or as an ASGI application using a server such as Daphne or Uvicorn. You can find more information on how to deploy Django with different web servers at https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/.
Chapter 17, Going Live, explains how to set up a production environment for your Django projects.
Let’s open the settings.py file and take a look at the configuration of the project. There are several settings that Django includes in this file, but these are only part of all the available Django settings. You can see all the settings and their default values at https://docs.djangoproject.com/en/5.0/ref/settings/.
Let’s review some of the project settings:
DEBUG is a Boolean that turns the debug mode of the project on and off. If it is set to True, Django will display detailed error pages when an uncaught exception is thrown by your application. When you move to a production environment, remember that you have to set it to False. Never deploy a site into production with DEBUG turned on because you will expose sensitive project-related data.ALLOWED_HOSTS is not applied while debug mode is on or when the tests are run. Once you move your site to production and set DEBUG to False, you will have to add your domain/host to this setting to allow it to serve your Django site.INSTALLED_APPS is a setting you will have to edit for all projects. This setting tells Django which applications are active for this site. By default, Django includes the following applications:django.contrib.admin: An administration site.django.contrib.auth: An authentication framework.django.contrib.contenttypes: A framework for handling content types.django.contrib.sessions: A session framework.django.contrib.messages: A messaging framework.django.contrib.staticfiles: A framework for managing static files, such as CSS, JavaScript files, and images.MIDDLEWARE is a list that contains middleware to be executed.ROOT_URLCONF indicates the Python module where the root URL patterns of your application are defined.DATABASES is a dictionary that contains the settings for all the databases to be used in the project. There must always be a default database. The default configuration uses a SQLite3 database.LANGUAGE_CODE defines the default language code for this Django site.USE_TZ tells Django to activate/deactivate timezone support. Django comes with support for timezone-aware datetimes. This setting is set to True when you create a new project using the startproject management command.Don’t worry if you don’t understand much about what you’re seeing here. You will learn more about the different Django settings in the following chapters.
Throughout this book, you will encounter the terms project and application over and over. In Django, a project is considered a Django installation with some settings. An application is a group of models, views, templates, and URLs. Applications interact with the framework to provide specific functionalities and may be reused in various projects. You can think of a project as your website, which contains several applications, such as a blog, wiki, or forum, that can also be used by other Django projects.
Figure 1.4 shows the structure of a Django project:
Figure 1.4: The Django project/application structure
Let’s create our first Django application. We will build a blog application from scratch.
Run the following command in the shell prompt from the project’s root directory:
python manage.py startapp blogThis will create the basic structure of the application, which will look like this:
blog/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.pyThese files are as follows:
__init__.py: This is an empty file that tells Python to treat the blog directory as a Python module.admin.py: This is where you register models to include them in the Django administration site—using this site is optional.apps.py: This includes the main configuration of the blog application.migrations: This directory will contain database migrations of the application. Migrations allow Django to track your model changes and synchronize the database accordingly. This directory contains an empty __init__.py file.models.py: This includes the data models of your application; all Django applications need to have a models.py file but it can be left empty.tests.py: This is where you can add tests for your application.views.py: The logic of your application goes here; each view receives an HTTP request, processes it, and returns a response.With the application structure ready, we can start building the data models for the blog.
Remember that a Python object is a collection of data and methods. Classes are the blueprint for bundling data and functionality together. Creating a new class creates a new type of object, allowing you to create instances of that type.