'How can I add a prefix to ALL my urls in django

I want to add a prefix to all my URLs in Django python (3.0.3) so all my path that I defined in urls.py will start with "api/". The prefix may be changed in the future so I want it to be dynamic as possible



Solution 1:[1]

Just add a nested prefix path() to the urlpatterns in your project/project/urls.py

beforre:

urlpatterns = [
    path('yourapp/', include('yourapp.urls')),
    path('admin/', admin.site.urls),
]

after:

urlpatterns = [
    path('django_prefix/', include([
            path('yourapp/', include('yourapp.urls')),
            path('admin/', admin.site.urls),
    ])),
]

You can choose which should have the prefix or not.

And you should add the prefix to STATIC_URL, MEDIA_URL in your project/project/settings.py (to ensure the static and media files under the prefix too)

STATIC_URL = 'django_prefix/static/'
MEDIA_URL = 'django_prefix/media/'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1