解决django.core.exceptions.ImproperlyConfigured: Cannot import 'book.apps.users'. Check that

2022/4/30 14:12:38

本文主要是介绍解决django.core.exceptions.ImproperlyConfigured: Cannot import 'book.apps.users'. Check that,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

问题

在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',
]


这篇关于解决django.core.exceptions.ImproperlyConfigured: Cannot import 'book.apps.users'. Check that的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程