- We define a custom
app_namein an app’surls.pyand use a differentnamespacein theinclude()method inurls.pyof the base project? - The
app_name(or customapp_name) and thenamespaceare different or one of them matches the other? - We have multiple apps where:
Some apps have the samenamespacedefined in the baseurls.pyas one of the customapp_names.
Some apps use entirely differentapp_nameandnamespacecombinations.
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?