Think of instance namespace as your nickname and application namespace as your real name.
People can have many nicknames for you, but your real name doesn't change.
Django uses the application namespace (your real name) to reverse urls, cause as an application you should not care how many instance namespaces (nicknames) there are.
But to differentiate between one instance and the next, you will use the instance namespaces (nicknames) in urlconfs.
Let's say your app deals with customers and store employees. Both of these have physical addresses and you might want to use an app that just handles all address information: forms, validation, model structure, geo location etc. Our fictional app is a reusable django app called "django_super_address".
Your root url conf may look like this:
urlpatterns = [
path('customer/address/',
include('django_super_address.urls',
namespace='customer_address')
),
path('employee/address/',
include('django_super_address.urls',
namespace='employee_address')
),
]
This includes the same urls below different URL paths, pointing to the same app, using different namespaces (nicknames).
Within django_super_address
, it has defined it's application namespace:
# File: django_super_address/urls.py
from . import views
app_name = 'django_super_address'
urlpatterns = [
path('create/', views.SomeAddressCreateView.as_view(),
name='address_create'),
path('create/success/', views.SuccessView(),
name='address_create_success'),
...
]
And in views it uses:
# File django_super_address/views.py
class SomeAddressCreateView(generic.CreateView):
def success_url(self):
return reverse(
'django_super_address:address_create_success'
)
...
When Django gets the URL `/employee/address/create/':
- It sets the instance namespace to
employee_address
- It reads the urls from
django_super_address
- It sets the application namespace to
django_super_address
and
associates the urls.py with that application namespace.
In the view:
- It reverses the URL by looking at the urls file associated with the application namespace (real name) and reverses the name into:
create/success
- It then goes back up the chain, now using the instance namespace (nickname) to prepend the rest of the url:
root
+ employee/address/
+ create/success/
= /employee/address/create/success/
And there you have it: nicknames for urlconfs and application namespaces for reverse!
namespace
argument in the url conf to determine where the lookup should match the view function name i.e'admin:index'
admin being the application namespace as admin is an application in the django project. and index a view function in the views.py module. The namespace can be changed by default it uses the app module with theapp_name
or that can be added to the url.py in case of Django2.0app_name = 'your_app_name'
– Liken