Search code examples
pythondjangoroutesdjango-urlsdjango-2.2

Django 2.2 : Is there any way to remove app name from admin urls?


I am using Django Built-in Admin panel, is there any way to remove app name from urls?

If I want to access User listing, it redirects me to 27.0.0.1:8000/admin/auth/user/ can I make it 27.0.0.1:8000/admin/user/ without the app name auth?

Thanks,


Solution

  • As documented here you can create a custom AdminSite and override the get_urls method. This simple code should to the job:

    In your common.admin.py

    from django.contrib.admin import AdminSite
    
    class MyAdminSite(AdminSite):
    
        def get_urls(self):
            urlpatterns = super().get_urls()
            for model, model_admin in self._registry.items():
                urlpatterns += [
                    path('%s/' % (model._meta.model_name), include(model_admin.urls)),
                ]
            return urlpatterns
    
    
    my_admin = MyAdminSite('My Admin')
    
    my_admin.register(YourModel)
    ...
    

    Note that you register your models with the new custom AdminSite instance.

    Then in your projects urls.py

    from common.admin import my_admin as admin
    from django.urls import path
    
    urlpatterns = [
        path('admin/', admin.urls),
        # Your other patterns
    ]