简体   繁体   中英

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,

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
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM