Django 1.9 can not find newapp

2019-03-03 11:13发布

问题:

I have worked with django 1.7 and recently started new project with django 1.9. The major difference with starting a new app was that new app is created with apps.py file. I read on django docs that now you have to use

INSTALLED_APPS = [
    'newapp.apps.NewAppConfig',
    ...
]

(old was just 'newapp')

In my new project i have all my apps in a new directory called 'myapps'. But if i use 'myapps.newapp.apps.NewAppConfig' than django gives error ImportError: No module named newapp But if i use the old way i.e.

INSTALLED_APPS = [
    'myapps.newapp',
    ...
]

Than it works perfectly without error but configs in apps.py file may not be applied ( i don't know how it works ).

So which is the right way to put newapp in INSTALLED_APPS settings for django 1.9 when you got all your new apps in another directory like "myapps" with apps.py config file also working?

回答1:

If your apps directory is in the myapps directory, then check the following:

  1. Your myapps directory should have an __init__.py

  2. Include myapps in the path when you include the app in INSTALLED_APPS, i.e. 'myapps.newapp.apps.NewAppConfig',

  3. Include myapps in the app config's name attribute:

    class NewAppConfig(AppConfig):
        name = 'myapps.newapp'
    


回答2:

If your app is inside the "myapps" directory, you still need to include that in the path: "myapps.newapp.apps.NewAppConfig".