0

I am trying to move all the apps that I have in my Django application to a folder call apps:

The structure that I have right now is:

miApp
-accounts #App folder
-participation #App folder
-administration #App folder

-miapp 
--wsgi.py
--urls.py
--settings
---development.py
---production.py
---__init__.py
-static

And I would like to have something like this

miApp
-apps #New folder for the apps
--accounts #App folder
--reputation #App folder
--participation #App folder
--administration #App folder
--__init__.py
-miApp 
--wsgi.py
--urls.py
--settings
---common.py #Common settings for develpment and production
---development.py
---production.py
---__init__.py
-static
...

I tried already to move them all to the same folder,

I did the following change in miApp.urls

path('', views.inicio, name='inicio'),
path('admin/', admin.site.urls),
path('cuentas/', include('apps.accounts.urls')),
path('participation/', include('apps.participation.urls')),
path('administracion/', include('apps.administration.urls')),

And also this, in the settings of common.py

DJANGO_APPS = ['django.contrib.admin',
                'django.contrib.auth',
                'django.contrib.contenttypes',
                'django.contrib.sessions',
                'django.contrib.messages',
                'django.contrib.staticfiles',
                'django.contrib.sites'
               ]
THIRD_PARTIES = [# Django AllAuth
                'allauth',
                'allauth.account',
                'allauth.socialaccount',
                'allauth.socialaccount.providers.facebook',
                'allauth.socialaccount.providers.google',
                'allauth.socialaccount.providers.twitter',
                # Django guardian
                'guardian',
                #Django meta
                'meta',
                # Django push notifications
                'push_notifications',
                ]

MI_APP_APPS = [# MyVoto Instaled apps
                'apps.accounts.apps.AccountsConfig',
                'apps.participation.apps.ParticipationConfig',
                'apps.administration.apps.AdministrationConfig']

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTIES + MI_APP_APPS

I have the following config classes in apps.py

class AccountsConfig(AppConfig):
    name = 'apps.accounts'

class AdministrationConfig(AppConfig):
    name = 'apps.administration'

class ParticipationConfig(AppConfig):
    name = 'apps.participation'

And I am getting the following error:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x107afa488>
Traceback (most recent call last):
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/apps/config.py", line 143, in create
    app_module = import_module(app_name)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'accounts'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
    autoreload.raise_last_exception()
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 327, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "/Users/robertofernandez/PycharmProjects/miApp/venv/lib/python3.6/site-packages/django/apps/config.py", line 147, in create
    app_name, mod_path, cls_name,
django.core.exceptions.ImproperlyConfigured: Cannot import 'accounts'. Check that 'apps.accounts.apps.AccountsConfig.name' is correct.

I have the following in apps.accounts.apps.py

class AccountsConfig(AppConfig): name = 'accounts'

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

I think that's not a good practice to move apps folders this way but did you try to add a __init__.py file in apps?

miApp
-apps #New folder for the apps
--accounts #App folder
--reputation #App folder
--participation #App folder
--administration #App folder
--__init__.py

EDIT
Ithink the problem is the location of your apps.py file. From what you have in INSTALLED_APPS, it tries to get your config classes from an apps.py in every app folder.

Check this answer, the guy describes exactly what you're trying to do.
Really make sure you have the __init__.py file inside your apps folder and inside each app folder.

  1. Move your apps
  2. Change your prefix your apps in INSTALLED_APPS and urls.py by apps.
Cyrlop
  • 1,894
  • 1
  • 17
  • 31
0

The problem was that as I am using a custom user model in my common.py I had this:

AUTH_USER_MODEL = 'accounts.User'

With the changes I was doing, I also add the prefix apps to this setting, don't know why this setttings it works different, but instead of adding the prefix apps I had to leave it like it was before.

I think it maybe cause of the initial migration, that some how as is in the initial migration where the custom model is set up, probably the route is store somewhere and that maybe the thing causing the problem