问题

在django的apps下有多个子应用,如users,一开始在settings下面这样配置

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'apps.users.apps.UsersConfig',
]

无论是启动还是创建超级用户,都会报错

django.core.exceptions.ImproperlyConfigured: Cannot import 'book.apps.users'. Check that 'apps.users.apps.UsersConfig.name' is correct.

解决办法

1.在settings里加入代码

import sys, os
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))

2.然后直接在INSTALLED_APPS中使用子应用的名字apps.users

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'apps.users',
]