The Django architecture
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 the data definition 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.