I overwrote the admin site to customize a little bit on the main page of the admin and now get_urls
is never called. Note, that I defined the
custom_admin.py:
class TestAdminSite(admin.AdminSite):
index_template = "Test/admin_index.html"
def index(self, request, extra_context=None):
extra_context = extra_context or {}
print("test") # <-- this gets printed!
return super().index(request, extra_context)
def get_urls(self):
urls = super().get_urls()
print("test 2") # <-- not printed
return urls
custom_admin_site = TestAdminSite(name="custom_admin")
admin.py:
from .custom_admin import custom_admin_site
in the projects urls.py I did:
from testapp.admin import custom_admin_site
urlpatterns = [..., path("admin/", custom_admin_site.urls), ... ]
but as commented above, get_urls
is never called. why?
This is probably because the URL resolver loads lazily. Indeed, as long as you don't make a request, or you don't have an app that for example loads the URL resolver, it will not inspect the paths, and thus not call .get_urls()
since the root urls.py
is not read, and thus all consequent paths.
If you thus want to urls to be loaded, you need to somehow force the resolver to get loaded. The easiest way is probably to make a HTTP request. If you want to run this in a test, that is of course not always an option. You can force loading the resolver with:
from django.urls import get_resolver
get_resolver()