Research Preamble: What have I tried?
I'm new to Django and learning my way through while trying to follow best practices. I'm working my way through Two Scoops of Django 3.x. Any questioning of structure should be directed to the authors.
I'm hopeful that the question is solid enough to not be deleted in the first few minutes as I'm really stuck on this one. If this question is to be deleted I would appreciate guidance on how to improve how I ask questions in the future. I've read the how to ask good questions blog and am hopeful I'm following this guidance. If I did miss something please do more than provide a link to the blog.
- Please note that I have researched this question and the closest I came was a Q&A for Django 1.7 here: Django 1.7 app config ImportError: No module named appname.apps: This solution did not work for me (although it did give me a good path to try). Interestingly, they chose the exact same application structure as I did, over 5 years earlier. Please note that providing the project name in config did not solve this for me as it did the OP.
Other questions that either did not yield the needed solution:
- django error - ImportError: No module named apps - I tried this answer and it did not work for me
- Django Mezzanine ImportError: No module named apps - I validated that I am pointing to the specifically created Virtual Environment in PyCharm. As per Two Scoops of Django 3.2 I installed a separate envs folder and have activated a dedicated lanesflow_env for this project. PyCharm appears to have completed this since MIDDLEWARE in base.py (settings.py) has all django middleware apps imported successfully.
Other questions that were not directly related (at least as far as I can tell given my limited experience):
- Django - ImportError: No module named apps
- CherryPy 3.2 ImportError: No module named wsgiserver3
- ImportError: Module "whitenoise.middleware" does not define a "WhiteNoiseMiddleWare" attribute/class
- ImportError: No module named apps from django-grappelli
Relevant Information to the Question
This is the project tree:
Contents of articles/__init__.py
:
default_app_config = 'LanesFlow.apps.articles.apps.ArticlesConfig'
Contents of articles/apps.py
:
from django.apps import AppConfig
class ArticlesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.articles'
class ArticlesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'LanesFlow.apps.articles' # This was checked per the answer to the researched question
Installed app in base.py
(settings.py
):
INSTALLED_APPS = [
'apps.articles.ArticlesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Traceback on error:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\Carewen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 221, in create
raise ImportError(msg)
ImportError: Module 'apps.articles' does not contain a 'ArticlesConfig' class.
Any assistance would be welcomed.
EDIT
I've reverted to the last working version once more. I've found where it goes wrong. When I try to add 'articles.app.ArticleConfig' to INSTALLED_APPS
things go wrong. The thing is, this leaves me more confused. I'm trying to add templates. The process I'm following is:
- Create templates directory within the root directory, with articles as the sub-directory (following the recommended structure in Two Scoops - The logic behind this is to allow the creation of base templates that can be used for all applications - which is what I want to accomplish with this approach).
- At this point I get the
ImportError
. However, theapps.py
file does contact the classArticlesConfig(AppConfig)
Also in base.py
(settings.py
):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'apps.templates'], # Two scoops add. Was []
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I remain confused. My inexperience with Django is really showing here. I'm not certain what I'm doing wrong with the addition to INSTALLED_APPS
. My instinct is pointing me to exploring other aspects of settings.py to see if I'm pointing someone incorrectly.