Search code examples
djangodjango-urlsdjango-settings

Issue loading admin.site.urls after changing the settings folder


I re-organized Django,the following way:

config
 - settings
     - base.py
     - local.py
 urls.py
 wsgi.py   

In base.py/local.py:

ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'

In manage.py I changed:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

In wsgi.py I changed:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

I have the following error on runserver:

\django\contrib\admin\sites.py", line 269, in get_urls
    path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'AccountAdmin' object has no attribute 'urls'

It is related to this line:

 path('admin/', admin.site.urls), # Django 2.0 syntax

If I comment that line I get the following error:

django\contrib\admin\sites.py", line 79, in check
    if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'AccountAdmin' object has no attribute 'model

The app admin is in installed app, I don't know what is creating this issue.


Solution

  • Hmm... Several things happen here. One thing at a time:

    Under your settings dir put an __init__.py file with the following contents in it:

    from .base import *
    
    try:
        from .local import *
        LIVE = False
    except ImportError:
        LIVE = True
    
    if LIVE:
        try:
            from .production import *
        except ImportError:
            pass
    

    By putting this inside the __init__.py file, you can reference to your settings file simply with 'config.settings', leaving local or production unreferenced (the __init__.py will handle them).

    Now that this is out of way, change both uwsgi.py and manage.py to:

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
    

    Assuming that you have done all that, it should work (that's how I structure my projects for years and had never any problems). Otherwise, please update your question with project structure and base.py and local.py contents to work it out.