BeginnerCoder

Lesson 25 — Ultimate: mini URL router

Boss level: URLs with parameters

Real URLs carry data: /user/7 means "user number 7". Django writes this as path("user/<int:id>/", view). Underneath, it is a regular expression (a text pattern): (\d+) matches one or more digits and captures them, and m.groups() hands the captured text to the view.

re.match(r"^/user/(\d+)$", "/user/7").groups()   # ('7',)

Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.

Why it matters: every framework starts with routing — now you know how it really works.

Want it another way?
Task

Add a route for "/post/<slug>" (letters, digits and dashes: [\w-]+) that returns "post " + slug.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Django Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.