The Request Cycle

What this is. Django is a framework for building web applications in Python. It handles the parts every site needs, so you write the parts that make yours different.

Everything it does happens in the same five steps, in the same order.

The five steps.

1. A URL arrives, and urls.py decides which view handles it.

2. That view runs: it reads the request, decides what is needed, and asks for it.

3. The models fetch or change data, through the ORM rather than SQL you write.

4. A template turns that data into HTML.

5. The response goes back to the browser.

When something is wrong, work out which step before changing anything. A 404 is step one. A 500 is usually two or three. A blank space where a value should be is almost always four.

✦ Django calls things by different names

What most frameworks call a controller, Django calls a view. What they call a view, Django calls a template. Django's own name for the pattern is MVT: Model, View, Template. This matters when reading anything written for another stack, because the same word means a different thing.

Why use a framework at all. Sessions, password hashing, form validation, protection against SQL injection and cross-site scripting, database migrations, an admin interface. Every one of those is a solved problem with a well-understood correct answer, and every one is dangerous to get wrong.

Django's opinion is that you should not be rewriting them. Fighting that opinion is the most expensive way to use it.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Projects, Apps, and manage.py →
← Back