-
Book Overview & Buying
-
Table Of Contents
Django 5 By Example - Fifth Edition
By :
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.2/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:
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.4 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.4: The Django architecture
Quick tip: Need to see a high-resolution version of this image? Open this book in the next-gen Packt Reader or view it in the PDF/ePub copy.
The next-gen Packt Reader and a free PDF/ePub copy of this book are included with your purchase. Scan the QR code OR visit https://packtpub.com/unlock, then use the search bar to find this book by name. Double-check the edition shown to make sure you get the right one.

This is how Django handles HTTP requests and generates responses:
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 introduced several key features that you will use in the examples of this book. This version also deprecated certain features and eliminated previously deprecated functionalities. Here are some of the new major features introduced:
GENERATED ALWAYS SQL syntax..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 came 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.
Since then, Django 5.1 and 5.2 have been released. Django 5.1 dropped support for Python 3.8 and 3.9, and introduced the following major features:
login_not_required() decorator.Meanwhile, Django 5.2 introduced the following new major features:
BoundField on forms which makes adding classes for styling much simpler. For further information on the changes introduced in 5.1 and 5.2, see the Appendix. As a time-based release, there are no drastic changes in Django 5, making it straightforward to upgrade Django 4 applications to the 5.2 release.
You can access the Appendix through the following link: https://packt.link/1g7Af.
If you want to quickly upgrade an existing Django project to the 5.2 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.