Admin Site URL Patterns

I have been wondering and searching for the last few days and I still haven’t found a solution.

The URL structure of the admin page is app_label/model_name. Can I use the default admin page and change or translate this URL structure?

New URL patterns can be added by overriding the get_urls method but the URL patterns that django creates cannot be changed?

It depends upon specifically what changes you want to make.

The entry in the urls.py file typically looks something like this:

path('admin/', admin.site.urls),
               ^^^^^ -> django.contrib.admin
                     ^^^^ -> sites.py, instance of DefaultAdminSite
                          ^^^^ -> generated by the `get_urls` method

Some changes are trivial, such as changing the base component of the url from admin to something else. Change admin/ to whatever/, and the root component is changed.

Other changes are going to require more work. Nothing requires you to use this function to create the URLs, as long as you adhere to the requirements of the views being invoked - meaning that the right url names are assigned to the right views, and that the object_id is included (where appropriate) within the url.

See the docs and the source code for get_urls for more details on this.

You’re correct that the default URL structure of the Django admin follows the format app_label/model_name (e.g., /admin/app_label/model_name/).

While you can add new URL patterns by overriding the get_urls method in your custom admin class, the default URL patterns Django generates for the admin page are hardcoded and cannot be directly modified.

I do have a couple of theories on a workaround that, I haven’t tested this:

create some kind of middleware
If you simply want to map custom URLs to the default admin views without modifying the core behavior, you can use middleware or add redirects in your URL configuration:

from django.urls import path
from django.http import HttpResponseRedirect

def custom_redirect_view(request):
    return HttpResponseRedirect('/admin/new_app_label/new_model_name/')

urlpatterns = [
    path('admin/custom-path/', custom_redirect_view),
]

Use Reverse Proxying or a URL Rewrite Rule

If you’re deploying on a server like Nginx or Apache, you can use URL rewrite rules to translate or map URLs externally without modifying Django’s admin configuration.

location /admin/custom-path/ {
    rewrite ^/admin/custom-path/(.*)$ /admin/app_label/model_name/$1 break;
    proxy_pass http://your_django_backend;
}

note: theory I just have

This is not an accurate statement.

You are not limited to adding new urls.

The urls themselves are not “hardcoded”, they are generated from the app definitions.

Replacing the system-provided get_urls method with your own implementation will assign your own defined version.

There is no need to create custom middleware or proxy definition for this.

1 Like

thank you for the insight

I was just wondering how I can change the app_label/model_name structure.

For example, could the structure mysite.com/admin/app_label/model_name be changed to mysite.com/admin/example?

While reviewing Django code, I noticed the following in the get_urls method of the AdminSite class;

urlpatterns += [
    path(
        "%s/%s/" % (model._meta.app_label, model._meta.model_name),
        include(model_admin.urls),
    ),
]

I created my own Admin Site class that inherits the Django Admin Site class and override the get_urls method and change the path in urlspattern as follows;

def get_urls(self):
    from django.urls.resolvers import RoutePattern

    urlpatterns = super().get_urls()

    for url in urlpatterns:
        if ("app_label/model_name" == str(url.pattern)):
            url.pattern = RoutePattern("example/")
            break
     
    return urlpatterns

It’s worked. But I don’t know how accurate this is.