问题描述
我已经在django 1.7工作,最近开始使用django 1.9开始新的项目。启动新应用程序的主要区别是,使用apps.py文件创建了新的应用程序。我阅读了,现在你必须使用
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',
...
]
(旧只是'newapp'
)
在我的新项目中,我将所有应用程序都放在一个名为'myapps'的新目录中。
但是如果我使用
'myapps.newapp.apps.NewAppConfig'
比django给出错误 ImportError:没有名为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',
...
]
比它完全没有错误,但是在apps.py文件中的配置可能不会被应用(我不知道它是如何工作的)。
Than it works perfectly without error but configs in apps.py file may not be applied ( i don't know how it works ).
那么哪个是正确的方式将newapp放在INJALLO_APPS设置中,当您将所有新的应用程序与另一个目录(如myapps)一起使用到apps.py配置文件时也可以正常工作?
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?
推荐答案
如果您的应用目录位于 myapps
目录中,请检查以下内容:
If your apps directory is in the myapps
directory, then check the following:
-
您的
myapps
目录应该有一个__ init __。py
在 INSTALLED_APPS中包含应用程序时,在路径中包含
,即 myapps
'myapps.newapp.apps.NewAppConfig',
在应用配置的属性:
Include myapps
in the app config's name
attribute:
class NewAppConfig(AppConfig):
name = 'myapps.newapp'
这篇关于Django 1.9找不到newapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!