Configure your app's URLconf to automatically use a URL namespace by setting the app_name
attribute:
# In <myapp>/urls.py
from django.conf.urls import url
from .views import overview
app_name = 'myapp'
urlpatterns = [
url(r'^$', overview, name='overview'),
]
This will set the application namespace to 'myapp'
when it is included in the root URLconf>. The user of your reusable app does not need to do any configuration other than including your URLs:
# In <myproject>/urls.py
from django.conf.urls import include, url
urlpatterns = [
url(r'^myapp/', include('myapp.urls')),
]
Your reusable app can now reverse URLs using the application namespace:
>>> from django.urls import reverse
>>> reverse('myapp:overview')
'/myapp/overview/'
The root URLconf can still set an instance namespace with the namespace
parameter:
# In <myproject>/urls.py
urlpatterns = [
url(r'^myapp/', include('myapp.urls', namespace='mynamespace')),
]
Both the application namespace and instance namespace can be used to reverse the URLs:
>>> from django.urls import reverse
>>> reverse('myapp:overview')
'/myapp/overview/'
>>> reverse('mynamespace:overview')
'/myapp/overview/'
The instance namespace defaults to the application namespace if it is not explicitly set.