URL Namespacing / reverse() (Django)
URL namespacing in Django scopes URL names to an app so 'blog:detail' and 'shop:detail' don't collide. You set app_name in urls.py and reverse names with reverse() in Python or {% url %} in templates instead of hard-coding paths.
Django
Hard-coded URLs are fragile. Django lets you give each pattern a name and then look up the actual path by that name, so changing a URL in urls.py automatically updates every link. reverse('blog:detail', args=[5]) returns the path; {% url 'blog:detail' 5 %} does the same in templates.
Namespacing prevents name collisions across apps. Setting app_name = 'blog' in blog/urls.py turns name='detail' into the qualified name 'blog:detail'. Without it, two apps both naming a pattern 'detail' would clash.
Models often expose get_absolute_url() using reverse(), giving a single source of truth for an object's URL that the admin and templates can both use.
Syntax
reverse("blog:detail", args=[pk])
{% url 'blog:detail' pk %}Examples
Python
from django.urls import reverse
# urls.py has app_name = 'blog' and name='detail'
url = reverse("blog:detail", args=[42]) # -> '/blog/post/42/'
# in a model:
def get_absolute_url(self):
return reverse("blog:detail", args=[self.pk])