# How does Django handle URL routing when

**URL:** <https://forum.djangoproject.com/t/how-does-django-handle-url-routing-when/37691>\
**Category:** Forms & APIs\
**Created:** [January 4, 2025, 8:23pm UTC](https://forum.djangoproject.com/t/how-does-django-handle-url-routing-when/37691 "2025-01-04T20:23:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iradukunda-Fils](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/iradukunda-fils/32/26098_2.png) [@Iradukunda-Fils](https://forum.djangoproject.com/u/Iradukunda-Fils)\
**Post date:** [January 4, 2025, 8:23pm UTC](https://forum.djangoproject.com/t/how-does-django-handle-url-routing-when/37691/1 "2025-01-04T20:23:34Z")

</div>

1. We define a custom `app_name` in an app’s `urls.py` and use a different `namespace` in the `include()` method in `urls.py` of the base project?
2. The `app_name` (or custom `app_name`) and the `namespace` are different or one of them matches the other?
3. We have multiple apps where:  
Some apps have the same `namespace` defined in the base `urls.py` as one of the custom `app_name`s.  
Some apps use entirely different `app_name` and `namespace` combinations.

How does Django resolve such URL routing scenarios, and does it prioritize `namespace` over `app_name`?

```auto
project/
    urls.py
    settings.py
    ...
    blog/
        urls.py
    blog_comments/
        urls.py
    posts/
        urls.py

```

```auto
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')),
]

```

```auto
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'),
]

```

```auto
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'),
]

```

```auto
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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 4, 2025, 8:51pm UTC](https://forum.djangoproject.com/t/how-does-django-handle-url-routing-when/37691/2 "2025-01-04T20:51:41Z")

</div>

Side Note: When posting code here, enclose the code between lines of three  
backtick - ` characters. This means you’ll have a line of ```, then your code,  
then another line of ```. This forces the forum software to keep your code  
properly formatted. (I have taken the liberty of correcting your original posts.  
Please remember to do this in the future.)

Side Note 2: When you’re posting the contents of multiple files, it’s helpful if you identify each file with its name and the directory that it’s in.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 4, 2025, 9:09pm UTC](https://forum.djangoproject.com/t/how-does-django-handle-url-routing-when/37691/3 "2025-01-04T21:09:05Z")

</div>

The precedence and resolution of namespaced urls is documented at [URL namespaces](https://docs.djangoproject.com/en/5.1/topics/http/urls/#url-namespaces).

Read the list at [Reversing namespaced URLs](https://docs.djangoproject.com/en/5.1/topics/http/urls/#reversing-namespaced-urls) to see what’s looked at first.

Also, quoting the second sentence of the first paragraph for the definition of [instance namespace](https://docs.djangoproject.com/en/5.1/topics/http/urls/#term-instance-namespace):

> Instance namespaces should be unique across your entire project.

While Django may not throw an error in this situation, it’s _probably_ not going to give you the result you expect, unless you’re working with nested namespaces.
