Caspar
Caspar

Reputation: 41

Django ImportError: Module does not define attribute/class

I am trying to add a custom/overridden AdminSite because I need a different template for the Admin. I did everything as the Docs said: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#overriding-the-default-admin-site

project/admin.py

from django.contrib.admin import AdminSite 

class NewAdminSite(AdminSite):
    index_template = 'admin/index.html'
    site_header = 'Administration'

admin_site = NewAdminSite(name='newadmin')

project/apps.py

class NewAdminConfig(AdminConfig):
    default_site = 'project.admin.NewAdminSite'

I don't have any other apps, project is my root directory ofc I added admin_site.urls instead of admin.site.urls to urls.py, created a custom AdminConfig in apps.py, and added that new AdminConfig to installed Apps instead of django.contrib.admin.

The Problem is that I now receive this:

AdminSiteClass = import_string(apps.get_app_config('admin').default_site)

File "C:\Users\User.virtualenvs\ssccms-fGQRLLK4\lib\site-packages\django\utils\module_loading.py", line 24, in import_string ) from err

ImportError: Module "project.admin" does not define a "NewAdminSite" attribute/class

my folder structure:

manage.py

project

workdir

node_modules

locale

pipfile.lock

pipfile

wsgi

hope this is enough for folder structure, it is the django-shop cookiecutter project example structure

EDIT:

yes, it was definitely a circular import error. My workaround now is simply using the given admin.site by writing:

admin.site.index_template = 'admin/newadminindextemplate.html'

it works! Which is actually all that I wanted. But the Documentation says the following https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#root-and-login-templates

If you wish to change the index, login or logout templates, you are better off creating your own AdminSite instance.

I tried that:

https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#customizing-the-adminsite-class

instead of the overriding part I pasted in the first link in the question but now there is

django.urls.exceptions.NoReverseMatch: Reverse for 'cascade_texteditor_config' not found. 'cascade_texteditor_config' is not a valid view function or pattern name.

now my new question is:

is it necessary to create your own instance of the index template ? the Documentation says no explanation so are there any problems that I might run into at a later time by doing admin.site.index_template?

Upvotes: 4

Views: 4135

Answers (1)

ssobczak
ssobczak

Reputation: 1855

I've just hit a similar error. It's a bit confusing, in my case it was due to a circular import.

In my case the following helped:

  • convert imports from top of the file containing NewAdminSite class to local imports (inside of the function where they're needed
  • move the instantiation of admin_site variable to a separate file

Neither of those problems is actually visible in the code you've pasted, but may give you an idea what to look for.

Upvotes: 1

Related Questions