0

I have added a router property (an instance of DRF's SimpleRouter) to my AppConfig. I want to get a list of all installed apps in my urls.py file and add any app with the router property to my url patterns.

This is my urls.py file:

from django.conf.urls import url, include
from django.contrib import admin
from django.apps import apps

urlpatterns = [
    url(r'^admin/', include(admin.site.urls))
]

# Loading the routers of the installed apps and core apps
for app in apps.get_app_configs():
    if hasattr(app, 'router'):
        urlpatterns += app.router.urls

and this is an example of my modified AppConfig:

from django.apps import AppConfig
from .router import auth_router


class AuthConfig(AppConfig):

    name = "core.auth"
    # to avoid classing with the django auth
    label = "custom_auth"

    # router object
    router = auth_router

    def ready(self):
        from .signals import user_initialize, password_reset_set_token

default_app_config = 'core.auth.AuthConfig'

When I try the above solution, I end up getting a "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet." error message!

I've tried using the solutions proposed here but none of them worked!

Community
  • 1
  • 1
Kash Pourdeilami
  • 488
  • 2
  • 6
  • 24

1 Answers1

1

The error wasn't being caused by the urls.py folder, it was being by the AppConfig. I had to import the auth_router inside the ready method

from django.apps import AppConfig


class AuthConfig(AppConfig):

    name = "core.auth"
    # to avoid classing with the django auth
    label = "custom_auth"

    # router object
    router = None

    def ready(self):
        from .signals import user_initialize, password_reset_set_token
        from .router import auth_router
        self.router = auth_router

default_app_config = 'core.auth.AuthConfig'
Kash Pourdeilami
  • 488
  • 2
  • 6
  • 24