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.
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.