[Answered ]-Django- URL namespace

2👍

Imagine you have three apps in your project:

  • reviews
  • products
  • users

For each app you have a view in your views.py called DetailView.

class DetailView(generics.RetrieveUpdateDestroyAPIView):
    # Some logic here.

To call the view, you have a URL in your urls.py that you’ve named detail. This could get confusing!

url(r'^(?P<pk>\d+)$', views.DetailView.as_view(), name='detail')

To distinguish between the URLs (for example, in your templates using {% url ... %}) you can use namespacing.

url(r'^api/v1/reviews/', include(reviews_urls, namespace="reviews"))

When you namespace your URLs, you can reference them in templates or redirects as reviews:detail which simplifies your life and makes code more reusable.

def my_view(request):
    ...
    return redirect('reviews:detail', foo='bar')

Leave a comment