QamoosTech
Web & APIsRouting and viewsIntermediateDjangoPython

URL Reversing

PronunciationYOO-ar-EL rih-VER-sing

Definition

URL reversing builds a URL from the route's name (and its parameters) instead of writing the path by hand, so changing a route doesn't break every link.

Where you hear it

In Django (reverse(), {% url %}), redirects after a form, and refactors that rename paths.

Examples

  • Redirect with reverse('circle-detail', args=[circle.id]).
  • Name every route so you can reverse it.
  • The template uses reverse to build the link to the order page from its name.

Common mistake

Hardcoding paths in links and redirects. One path change then breaks pages across the site.

Don't confuse with

URL routing, which goes from a path to code. Reversing goes the other way, from a name to a path.

Say it at work

  • In a meeting or chatUse the route name, not the literal path.
  • In an email or ticketReverse the URL with the id as an argument.