Django和PyCharm中使用的constance包中的KeyError

js4nwp54  于 8个月前  发布在  PyCharm
关注(0)|答案(2)|浏览(76)

当我尝试使用Docker Compose通过Docker容器在Django项目上运行调试器配置时,出现以下错误。我在网上找不到AUTO_END_TIMEDELTA_MINS。知道是什么吗

Traceback (most recent call last):
2023-08-21T18:03:17.443221106Z   File "/usr/local/lib/python3.8/site-packages/constance/base.py", line 14, in __getattr__
2023-08-21T18:03:17.443224978Z     if not len(settings.CONFIG[key]) in (2, 3):
2023-08-21T18:03:17.443227740Z KeyError: 'AUTO_END_TIMEDELTA_MINS'

我的settings.py正在跟踪

import os
from datetime import timedelta

from hyperlocalgroceries.settings.constants import (
    BASE_DIR,
    TESTING,
    DEBUG,
    ENVIRONMENT,
)
from hyperlocalgroceries.settings.env import (
    SQL_ENGINE,
    SQL_DATABASE,
    SQL_USER,
    SQL_PASSWORD,
    SQL_HOST,
    SQL_PORT,
)

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ""

ALLOWED_HOSTS = ["*"]

FRAMEWORK_APPS = [
    "dal",
    "dal_select2",
    "postgres_metrics.apps.PostgresMetrics",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.gis",
    "django_extensions",
    "django.contrib.humanize",
    "django_celery_results",
    "corsheaders",
]

INTERNAL_APPS = [
    "authentication",
    "common",
    "orders",
    "location",
    "activity",
    "cse",
    "status",
    "shopkeeper",
    "shop",
    "orders_v2",
    "customer",
    "rider",
    "bots_v2",
    "property",
    "household",
    "subscription",
    "prep",
    "finance",
    "media",
    "payments",
    "products",
    "transactions",
    "corporate",
    "queues",
    "auction",
]

THIRD_PARTY_APPS = [
    "constance",
    "simple_history",
    "debug_toolbar",
    "django_tables2",
    "widget_tweaks",
    "constance.backends.database",
    "cacheops",
]

INSTALLED_APPS = FRAMEWORK_APPS + INTERNAL_APPS + THIRD_PARTY_APPS

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "simple_history.middleware.HistoryRequestMiddleware",
    "debug_toolbar.middleware.DebugToolbarMiddleware",
]

ROOT_URLCONF = "hyperlocalgroceries.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            os.path.join(BASE_DIR, "hyperlocalgroceries", "templates"),
            os.path.join(BASE_DIR, "cse", "templates"),
        ],
        "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 = "hyperlocalgroceries.wsgi.application"

if TESTING:
    DATABASES = {
        "default": {
            "ENGINE": "django.contrib.gis.db.backends.spatialite",
            "NAME": "_test_db",
        }
    }
else:
    DATABASES = {
        "default": {
            "ENGINE": SQL_ENGINE,
            "NAME": SQL_DATABASE,
            "USER": SQL_USER,
            "PASSWORD": SQL_PASSWORD,
            "HOST": SQL_HOST,
            "PORT": SQL_PORT,
        }
    }

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
        "OPTIONS": {
            "min_length": 9,
        },
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

LANGUAGE_CODE = "en-us"

TIME_ZONE = "Asia/Karachi"

USE_I18N = True

USE_L10N = True

USE_TZ = True

AUTH_USER_MODEL = "authentication.User"

LOGIN_URL = "/admin/login/?next=/cse/"

#
# DRF
#

if TESTING:
    REST_FRAMEWORK = {
        "COERCE_DECIMAL_TO_STRING": False,
        "DEFAULT_AUTHENTICATION_CLASSES": (
            "rest_framework.authentication.TokenAuthentication",
        ),
        "DEFAULT_THROTTLE_CLASSES": [
            "rest_framework.throttling.AnonRateThrottle",
            "rest_framework.throttling.UserRateThrottle",
        ],
        "DEFAULT_THROTTLE_RATES": {
            "anon": "100/minute",
            "user": "100/minute",
        },
    }
    INSTALLED_APPS.append("rest_framework.authtoken")
elif DEBUG:
    REST_FRAMEWORK = {
        "COERCE_DECIMAL_TO_STRING": False,
        "DEFAULT_PERMISSION_CLASSES": [
            "rest_framework.permissions.IsAuthenticated",
        ],
        "DEFAULT_AUTHENTICATION_CLASSES": (
            "rest_framework.authentication.TokenAuthentication",
            "rest_framework.authentication.SessionAuthentication",
            "drf_firebase_auth.authentication.FirebaseAuthentication",
        ),
        "DEFAULT_THROTTLE_CLASSES": [
            "rest_framework.throttling.AnonRateThrottle",
            "rest_framework.throttling.UserRateThrottle",
        ],
        "DEFAULT_THROTTLE_RATES": {
            "anon": "100/minute",
            "user": "100/minute",
        },
        "DATETIME_FORMAT": "%Y-%m-%d %H:%M:%S",
        "DATE_FORMAT": "%Y-%m-%d",
        "TIME_FORMAT": "%H:%M:%S",
        "EXCEPTION_HANDLER": "common.utils.rest_custom_exception_handler.custom_exception_handler",
    }
    INSTALLED_APPS.append("drf_firebase_auth")
    INSTALLED_APPS.append("rest_framework.authtoken")
else:
    REST_FRAMEWORK = {
        "COERCE_DECIMAL_TO_STRING": False,
        "DEFAULT_PERMISSION_CLASSES": [
            "rest_framework.permissions.IsAuthenticated",
        ],
        "DEFAULT_AUTHENTICATION_CLASSES": (
            "rest_framework.authentication.SessionAuthentication",
            "drf_firebase_auth.authentication.FirebaseAuthentication",
        ),
        "DEFAULT_THROTTLE_CLASSES": [
            "rest_framework.throttling.AnonRateThrottle",
            "rest_framework.throttling.UserRateThrottle",
        ],
        "DEFAULT_THROTTLE_RATES": {
            "anon": "100/minute",
            "user": "100/minute",
        },
        "DATETIME_FORMAT": "%Y-%m-%d %H:%M:%S",
        "DATE_FORMAT": "%Y-%m-%d",
        "TIME_FORMAT": "%H:%M:%S",
        "EXCEPTION_HANDLER": "common.utils.rest_custom_exception_handler.custom_exception_handler",
    }
    INSTALLED_APPS.append("drf_firebase_auth")

#
# CORS
#

if ENVIRONMENT == "PROD":
    CORS_ORIGIN_WHITELIST = [
    ]
    CSRF_TRUSTED_ORIGINS = [
    ]
else:
    CORS_ORIGIN_ALLOW_ALL = True

#
# STATIC FILES
#

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "hyperlocalgroceries", "static"),
    os.path.join(BASE_DIR, "cse", "static"),
]

#
# JWT
#

SIMPLE_JWT = {
    "ACCESS_TOKEN_LIFETIME": timedelta(days=30),
    "REFRESH_TOKEN_LIFETIME": timedelta(days=90),
    "ROTATE_REFRESH_TOKENS": True,
}

#
# DEBUG TOOL BAR
#

def show_toolbar(request):
    return DEBUG and not TESTING

DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK": show_toolbar,
}

#
# OTHERS
#

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

SIMPLE_HISTORY_REVERT_DISABLED = True

DJANGO_TABLES2_TEMPLATE = "django_tables2/bootstrap4.html"

SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
qkf9rpyu

qkf9rpyu1#

setting.py给出了关于这个问题的非常少的想法,你的模块constance导致了这个错误,这需要深入调试,但你可以尝试在你的settings.py中添加以下代码:

CONSTANCE_CONFIG = {
   'AUTO_END_TIMEDELTA_MINS': (False, "<Any String>"),
}
vd8tlhqk

vd8tlhqk2#

这个问题已经通过在PyCharm的调试器配置中使用整个settings文件夹而不是像通常那样使用单独的文件来解决,因为这就是项目的设置方式。我不知道这件事,因为我已经在2天内从PHP后端过渡到Django。
此外,当您连接到Docker Compose容器中定义的Python解释器时,请避免在PyCharm的Project Settings菜单中手动添加环境变量。PyCharm自己处理。这可能没有意义,但你可以评论你的问题,我会更新答案。
顺便说一句,我正在使用PyCharm 2023.2。

相关问题