( django.core.exceptions.ImproperlyConfigured: 无法导入 'apps.accounts'。检查 'mysite.apps.accounts.apps.AccountsConfig.name' 是否正确

( django.core.exceptions.ImproperlyConfigured: Cannot import 'apps.accounts'. Check that 'mysite.apps.accounts.apps.AccountsConfig.name' is correct

这是它的结构 帐户文件夹文件apps.py中的代码是

from django.apps import AppConfig
class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = "apps.accounts"

设置里面的代码是

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

我尝试将 'mysite.apps.accounts', 更改为 'mysite.apps.AccountsConfig', 并将 name = "apps.accounts" 更改为名称 = "accounts" 我是 Django 的新手,正在关注 How to make a website with Python and Django - MODELS AND MIGRATIONS (E04) tutorial. Around 16:17 是我在 vscode 终端输入 python manage.py makemigrate 时出现 error 的地方 错误是

ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'apps.accounts'. Check that 'mysite.apps.accounts.apps.AccountsConfig.name' is correct. Someone please help me.

要定义新应用程序,您不需要添加它的路径 app 就是这样的名字 'accounts' 注意 app 名称区分大小写

所以你安装的应用变成了:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
]

该解决方案非常违反直觉。您必须删除

class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = "accounts"

来自应用程序。py\accounts\apps\mysite。然后 运行 python manage.py makemigrations 和 2 个新模型 'UserPersona' 和 'UserProfile' 被创建。终端中的输出:

mysite\apps\accounts\migrations[=11=]01_initial.py
    - Create model UserPersona
    - Create model UserProfile