Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The Request–Response Flow  ›  Lesson

URL Patterns

URL patterns 12 minA URL pattern pairs a route string with a view, and Django walks the patterns in order, calling the first view whose route matches the requested path.

URL Patterns

A request has reached Django. Before any of your code runs, Django must decide which view will answer. It does this with a list of URL patterns, and the rules it follows are few and strict. Knowing them is the difference between "my page is a 404 and I don't know why" and a fix in ten seconds.

The rule

Top to bottom, first match wins, and the path is all it sees.

Here is the whole journey of a request, the map for this section:

browser → server → middleware (in) → URL patterns → view → middleware (out) → browser

The URL patterns step looks only at the path: no domain, no ?query, no leading slash. Each entry is a path():

# recipes/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.recipe_list),                 # /recipes/
    path("quick/", views.quick_recipes),         # /recipes/quick/
    path("<slug:slug>/", views.recipe_detail),   # /recipes/tomato-soup/
]

path(route, view) takes the route string, then the view to call. It can also take a name, which a later lesson puts to work. The part in angle brackets captures a piece of the URL and passes it to the view; that is the subject of the next lesson.

A route must match the whole remaining path. quick/ matches quick/ but not quick/extra/.

Trailing slashes are a Django convention. If someone types /recipes/quick without the slash, nothing matches; with the default APPEND_SLASH = True, Django redirects them to /recipes/quick/, which does.

The shortcut

Specific above general. Fixed routes like quick/ go above routes that capture anything, like <slug:slug>/. Otherwise the general pattern swallows the specific URL before its own pattern is ever reached.

Where does /recipes/quick/ go?Step 1 of 4
  1. Project level

    recipes/ matches through include(), leaving quick/.

When nothing matches, the response is 404. With DEBUG = True the error page lists every pattern that was tried, in order, which is the fastest way to spot an ordering bug.

Drills

Answer fast — trust the rule.

quick drill

For the request https://cookbook.example.com/recipes/?sort=new, what does URL matching see?

quick drill

A pattern list has <slug:slug>/ first and new/ second. What happens at /recipes/new/?

quick drill

With APPEND_SLASH = True, what happens at /recipes/quick if only quick/ exists?

quick drill

Does the route quick/ match the remaining path quick/extra/?

Remember

  • Top to bottom, first match wins, and only the path is matched.
  • Specific routes above general ones.
  • Trap: a capturing pattern placed too high swallows fixed URLs.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOrder matters

The recipes app has these patterns in this order: path("slug:slug/", views.recipe_detail) and then path("quick/", views.quick_recipes). Explain what happens when someone visits /recipes/quick/, why, and how to fix it.