- We define a custom
app_name
in an app’surls.py
and use a differentnamespace
in theinclude()
method inurls.py
of the base project? - The
app_name
(or customapp_name
) and thenamespace
are different or one of them matches the other? - We have multiple apps where:
Some apps have the samenamespace
defined in the baseurls.py
as one of the customapp_name
s.
Some apps use entirely differentapp_name
andnamespace
combinations.
How does Django resolve such URL routing scenarios, and does it prioritize namespace
over app_name
?
project/
urls.py
settings.py
...
blog/
urls.py
blog_comments/
urls.py
posts/
urls.py
from django.urls import path, include
urlpatterns = [
# Blog app with custom app_name and namespace
path('blog/', include(('blog.urls', 'blog'), namespace='blog_ns')),
# Blog comments app with custom app_name and namespace
path('comments/', include(('blog_comments.urls', 'comments_app'), namespace='blog')),
# Posts app with default app_name and namespace
path('posts/', include(('posts.urls', 'posts'), namespace='posts_post')),
]
from django.urls import path
# Define custom app_name
app_name = 'blog'
urlpatterns = [
path('list/', lambda request: None, name='list'),
path('detail/<int:id>/', lambda request: None, name='detail'),
]
from django.urls import path
# Define custom app_name
app_name = 'comments_app'
urlpatterns = [
path('list/', lambda request: None, name='list'),
path('add/', lambda request: None, name='add'),
]
from django.urls import path
# Default app_name aligned with the namespace
app_name = 'posts'
urlpatterns = [
path('list/', lambda request: None, name='list'),
path('detail/<int:id>/', lambda request: None, name='detail'),
]
What can happen when we used references: {% url 'blog:list" %}
or {% url 'posts_pos:list' %}
Am not understanding the logic behind this. Is there anyone can explain it clearly?