django.core.exceptions.AppRegistryNotReady:应用尚未加载.(django 2.0.1)(Python 3.6) [英] django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. (django 2.0.1)(Python 3.6)

查看:64
本文介绍了django.core.exceptions.AppRegistryNotReady:应用尚未加载.(django 2.0.1)(Python 3.6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试将Django应用程序(django 2.0.1)(Python 3.6)部署到pythonanywhere,这是一个没有模型,没有引导程序的简单组合应用程序.只是Django,HTML,CSS&Javascript.

It's my first time trying to deploy a Django app(django 2.0.1)(Python 3.6) to pythonanywhere, it is a simple portfolio app with no models, no bootstrap. Just Django, HTML, CSS & Javascript.

使用其bash控制台将其从Github存储库中拉到pythnanywhere后,我运行:

After pulling it from the Github repo onto pythnanywhere with their bash console, I run :

python manage.py migrate

&遇到此错误:

& was hit with this error :

Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 371, in 
execute_from_command_line
utility.execute()
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 216, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 36, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/core/management/commands/migrate.py", line 12, in <module>
from django.db.migrations.autodetector import MigrationAutodetector
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/db/migrations/autodetector.py", line 11, in <module>
from django.db.migrations.questioner import MigrationQuestioner
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-
packages/django/db/migrations/questioner.py", line 9, in <module>
from .loader import MigrationLoader
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 8, in <module>
from django.db.migrations.recorder import MigrationRecorder
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 9, in <module>
class MigrationRecorder:
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 22, in MigrationRecorder
class Migration(models.Model):
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "/home/Limerin555/.virtualenvs/projectenv/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我厌倦了在所有可能找到的解决方案中寻找解决方案,但是并没有什么真正的帮助,我什至尝试将这一行添加到我的settings.py中:

I tired looking for solutions everywhere I could possibly find but nothing really helps, I've even tried adding this line to my settings.py :

import django
django.setup()

在此行下面:

SECRET_KEY = os.environ.get("SECRET_KEY")

根据此帖子的建议,但无济于事.

as suggested from this post, but to no avail.

这是我的设置.py:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")

SECRET_KEY = os.environ.get('SECRET_KEY')

import django
django.setup()

DEBUG = False

ALLOWED_HOSTS = ["limerin555.pythonanywhere.com"]

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'limerin.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'limerin.wsgi.application'


DATABASE_PATH = os.path.join(BASE_DIR, 'db.sqlite3')

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': DATABASE_PATH,
    }
}


AUTH_PASSWORD_VALIDATORS = [
    {
    'NAME': 
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 
'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 
'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 
'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Singapore'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

我真的迷失了这一点,希望有人可以帮助您弄清这里真正的问题是什么.

I am really lost on this, hoping someone can help shed some light on what's the real problem here.

推荐答案

刚刚克服了类似情况.

您真正需要的就是这个:

All you really need is this:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")

然后是这些行:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

此后,您可以轻松导入模型而无需 AppRegistryNotReady:应用尚未加载.

After that you can easily import models without AppRegistryNotReady: Apps aren't loaded yet.

更新:这实际上就是项目文件夹中wsgi.py文件中的4行代码.

UPDATE: This is really exactly the 4 code lines from wsgi.py file in your project's folder.

用于DJANGO 3.0 在Django 3+中,需要一个额外的变量来解决同步/异步混乱:

FOR DJANGO 3.0 In Django 3+ an extra variable is needed to resolve sync/async confusing:

os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

这篇关于django.core.exceptions.AppRegistryNotReady:应用尚未加载.(django 2.0.1)(Python 3.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆