Named URLs
The rule. Give every route a name, and never write a path by hand anywhere else.
A hard-coded /post/3/ in a template works until the day you change the address, at which point it silently becomes a broken link. A named URL is looked up, so it changes with the route.
Three places you use the name.
In a template: {% url 'posts:detail' post.pk %}
In Python: reverse("posts:detail", args=[post.pk])
On the model: define get_absolute_url and then redirect(post) and {{ post.get_absolute_url }} both work.
The app_name prefix keeps names unique between apps, so posts:detail and accounts:detail can both exist.
Mark this lesson complete
Signed in, your progress follows you to every device.