Reputation: 635
I am writing a simple Django app - say, called base
- which has a few models. I am using Django's built-in admin site to manipulate the app's data. The admin is accessible via the default ^admin/
URL pattern.
Aside from Django's default INSTALLED_APPS
(minus django.contrib.sites
), base
is the only app installed in my project. Is there any way to remove base/
from the URL, such that I can access base
's models by simply using a path such as /admin/model/
instead of /admin/base/model/
?
I would ideally like django.contrib.auth
's models to still be accessible via /admin/auth/
.
Upvotes: 2
Views: 2181
Reputation: 5483
Customizing or overriding your default Django admin site is quite easy. Here's the Django documentation on this. The following is an example of overriding the default admin site.
Create an admin.py
in your Django project directory (if it's not there yet). Subclass the AdminSite.
To remove the 'appname' from the admin URLs override the get_urls()
function:
# myproject/admin.py
from django.contrib import admin
class MyAdminSite(admin.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
Creae an apps.py
in your project directory (if it's not there yet):
# myproject/admin.py
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'myproject.admin.MyAdminSite'
Register this in your settings.py
:
INSTALLED_APPS = [
...
'myproject.apps.MyAdminConfig', # replaces 'django.contrib.admin'
...
]
Upvotes: 1
Reputation: 599530
If you don't want base
in the URL, don't put it there. It's not there unless you have specifically asked it to be. Just create your URLs without that prefix.
Edit Apologies, I misread your question: I thought you were asking about your app's own views, not the sub-sections with admin.
This is tricky. One way of doing it would be to use the hooks for adding URLs to the base AdminSite as described in the docs. You will probably need to copy the code from the ModelAdmin.get_urls method and hard-code the model name, since there won't be a way of doing that automatically.
Upvotes: 1