Search code examples
djangodjango-urls

What is the purpose of app_name in urls.py in Django?


When include()ing urlconf from the Django app to the project's urls.py, some kind of app's name (or namespace) should be specified as:

  • app_namespace in include((pattern_list, app_namespace), namespace=None) in main urls.py

or

  • app_name variable in app's urls.py.

Since, I guess, Django 2, the second method is the preferred one Although I copy-pasted first function signature from Django 3 documentation. But that's not the main point.

My current understanding of namespace parameter of include() is that it's what I use when using reverse().

What is the purpose of app_name in app's urls.py or app_namespace in main urls.py?
Are these exactly the same thing?
How is it used by Django?

Existing questions (and answers) I've found here explain HOW I should specify it rather than WHY.


Solution

  • In this answer, I am taking the DRF package and its URL patterns. If you want to try any of the snippets mentioned in this answer, you must install (pip install djangorestframework) and add rest_framework to INSTALLED_APPS list.


    The application namespace can be set in two ways, [Ref: Django doc]

    1. in urls.py using app_name varibale.

      You can see that DRF has set the app_name in urls.py. Django will use this app_name as the application namespace only if we are included the patterns with module reference.

      That is, include(module, namespace=None)

      Example:

      urlpatterns = [  
       path('drf-auth/bare/', include('rest_framework.urls')),  
      ]
    2. in include((pattern_list, app_namespace), namespace=None) function using app_namespace parameter.

      In this method, you can set an additional app_namespace for the application, if you want.

      Most importantly, we are passing a pattern_list instead of module

      Example:

      from rest_framework.urls import urlpatterns as drf_urlpatterns
      
      urlpatterns = [  
         path('drf-auth/foo/', include((drf_urlpatterns, 'foo-app-namespace'))),  
      ]
      

    Complete Example

    from django.urls import path, include, reverse  
    from rest_framework.urls import urlpatterns as drf_urlpatterns  
      
    urlpatterns = [  
      path('drf-auth/bare/', include('rest_framework.urls')),  
      path('drf-auth/foo/', include((drf_urlpatterns, 'foo-app-namespace'))),  
      path('drf-auth/bar/', include((drf_urlpatterns, 'bar-app-namespace'))),  
    ]  
    
    print(reverse('rest_framework:login'))
    print(reverse('foo-app-namespace:login'))
    print(reverse('bar-app-namespace:login'))
    
    #results
    /drf-auth/bare/login/
    /drf-auth/foo/login/
    /drf-auth/bar/login/
    

    1. What is the purpose of app_name in app's urls.py or app_namespace in main urls.py?

    Both are used to set the application namespace. The app_name can be used as a default application namespace, if defined in the urls.py.

    1. Are these exactly the same thing?

    No.

    1. How is it used by Django?

    The application namespace and instance namespace are used to retrieve the URL path. In Django, whenever the reverse(...) function get executed, Django looking for an application namespace first, than any other. You can read more about how Django resolve the URL here, Reversing namespaced URLs