Django项目目录结构



在我的django项目结构,我希望我所有的django应用程序在一个单独的Apps文件夹,但当我包括它在settings.py它引发了一个错误,

raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'TestApp'. Check that 'Apps.TestApp.apps.TestappConfig.name' is correct.

INSTALLED_APPS = [
...
'Apps.TestApp'
]

当我只包含TestApp时,会引发no module named 'TestApp'错误。

INSTALLED_APPS = [
...
'TestApp'
]

如果你使用的是django版本<或者>

INSTALLED_APPS = [
...
'testapp.apps.TestappConfig' 
]

应用名称不应该是大写,否则你会得到错误。

如果你正在使用django>或者= 3,那么你也可以用它的原始名称注册你的应用程序。

你的应用程序注册为"标题"样式,这是不允许的。

您可以在settings.py文件中执行以下操作:

INSTALLED_APPS = [
... # other necessary apps here

# include your local apps you are creating for your project here
'testapp.apps.app_name', # assuming app_name is one of your apps
'testapp.apps.another_app',
'testapp.apps.third_custom_app'
]

然后在你的每个app文件夹中(你的models.py, views.py, urls.py等)包含一个apps.py文件,遵循以下模式:

from django.apps import AppConfig

class AppNameConfig(AppConfig): # note the formatting of this class name
default_auto_field = "django.db.models.BigAutoField"
name = "apps.app_name" # apps is the name of the directory where all your apps are located. 
# app_name is the name of the individual directory within your apps directory where this apps.py file is saved

最新更新