1.3 Understanding Modern Websites
HTTP is a stateless protocol: it doesn’t know who you are or where you’ve been. It knows only what you’ve just asked it for. In the early days of the Internet, each webpage on a site was a file, such as a text file or a PDF. Websites were static.
Today, many websites are dynamic. We now interact with websites: instead of just asking the server to send us a file, we write comments on videos, blog about the best web framework ever, and tweet cat pictures to our friends. To enable these activities, webpages must be generated (computed) for each user based on new and changing data. We’ve had to add a number of technologies on top of HTTP to determine state (such as sessions, which we’ll see in Chapter 19: Basic Authentication), and we now have entire languages and systems (like Django!) to make the dynamic generation of webpages as easy as possible. Our original HTTP loop now has an extra step between the request and response, as shown in Figure 1.3.
Figure 1.3: HTTP Request/Response Cycle Diagram
This dynamic generation of webpages is referred to as back-end programming, as opposed to front-end programming. Front-end programming involves creating the behavior of the webpage once it has already been generated by the back end. We can think of the combined experience in four steps:
- A user’s browser issues a request for a page.
- The server (or back end) generates a markup file (typically HTML) based on recorded information and information provided by the user; this file in turn points the user to download associated content such as JavaScript, which defines behavior, and Cascading Style Sheets (CSS), which define style such as color and fonts. The entire set of items defines the webpage.
- The server responds to the user’s browser with this markup file (typically causing the browser to then ask for the other content such as CSS and JavaScript).
- The user’s browser uses the information to display the webpage. The combination of HTML (content and structure), CSS (style), and JavaScript (behavior) provides the front-end part of the website.
While front-end programming certainly provides for a dynamic experience, the words dynamic webpage typically refer to a webpage that is computed on the back end. It can be difficult to distinguish the difference between front-end and back-end programming because modern websites strive to blur the difference to create a more seamless user experience. In particular, websites known as single-page applications blur this line to the point that step 2 is seriously mangled. However, the distinction is still important, as the HTTP protocol remains between the user and the server and the tools for front-end and back-end programming are typically quite different.
Furthermore, this book does not cover front-end programming. We will see how to serve static content such as CSS and JavaScript in Chapter 16: Serving Static Content with Django, but we will not write a single line of either. This book is dedicated entirely to back-end programming and generating dynamic webpages with Django.