Django2.1遇到 app_label and isn't in an application in INSTALLED_APPS 错误

徐奇逸
2023-12-01

Django在应用列表中添加新的应用时有时会出错

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'news_web',
    'DjangoUeditor',
    'mdeditor',
    'django_markdown',
    'markdownx',
    #'fluent_contents',
]

启动项目时报错为

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7efe941f08c8>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/core/management/commands/runserver.py", line 111, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/core/management/__init__.py", line 329, in execute
    autoreload.check_errors(django.setup)()
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/apps/registry.py", line 120, in populate
    app_config.ready()
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/py/super/news_web/admin.py", line 7, in <module>
    from fluent_contents.admin import PlaceholderFieldAdmin
  File "/usr/local/lib/python3.6/site-packages/fluent_contents/admin/__init__.py", line 14, in <module>
    from fluent_contents.admin.placeholdereditor import PlaceholderEditorAdmin, PlaceholderEditorInline, PlaceholderEditorBaseMixin
  File "/usr/local/lib/python3.6/site-packages/fluent_contents/admin/placeholdereditor.py", line 14, in <module>
    from fluent_contents import extensions
  File "/usr/local/lib/python3.6/site-packages/fluent_contents/extensions/__init__.py", line 12, in <module>
    from .pluginbase import PluginContext, ContentPlugin, HttpRedirectRequest
  File "/usr/local/lib/python3.6/site-packages/fluent_contents/extensions/pluginbase.py", line 13, in <module>
    from django.contrib.sites.models import Site
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/contrib/sites/models.py", line 80, in <module>
    class Site(models.Model):
  File "/usr/local/lib/python3.6/site-packages/Django-2.1-py3.6.egg/django/db/models/base.py", line 99, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

将不用的app注释掉也没有起到作用,上网查解决方案。找了半天在Stack Overflow上找找到一篇答案,解决方法如下:
在setting中增加

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
]

SITE_ID = 1

然后在启动项目即恢复正常
stack overflow链接地址

 类似资料: