Django 1.9 can not find newapp

耗尽温柔 提交于 2019-12-04 06:48:49

问题


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".



来源:https://stackoverflow.com/questions/37236047/django-1-9-can-not-find-newapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!