I am referring https://www.webforefront.com/django/namedjangourls.html to understand django urlconfs. I have encountered terms instance namespace and application namespace. I know about namespaces in urlsconfs. But I don't know the difference between them.
I referred django docs for it. It mentions that instance and app namespaces, comes into picture, when multiple instances of same app is used in django project.
But still, I am not able to understand it. I have googled out, but couldn't find any help on it.
Thanks in advance.
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/':
employee_address
django_super_address
django_super_address
and
associates the urls.py with that application namespace.In the view:
create/success
root
+ employee/address/
+ create/success/
= /employee/address/create/success/
And there you have it: nicknames for urlconfs and application namespaces for reverse!
From my understanding, it goes like this:
The application of Instance namespace might sound a bit confusing. Let me try to clarify if that's the case. Consider the example given in the Django docs.
urls.py:
from django.urls import include, path
urlpatterns = [
path('author-polls/', include('polls.urls', namespace='author-polls')),
path('publisher-polls/', include('polls.urls', namespace='publisher-polls')),
]
polls/urls.py:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
...
]
The namespaces are defined like this:
app_name
attribute in polls/urls.py
namespace
argument in urls.py
Here, both author-polls
and publisher-polls
are using the same polls.urls
to resolve their URLs. Without an Instance namespace, resolving a URL like 'polls:index' could create confusion on which URL it's intending to fetch. That's why Django has a set of protocol defined to fix this issue for all kinds of situations when you are attempting to reverse and resolve namespaced URLs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With