7

我目前正在使用 Heroku CI 运行我的 django 测试: python3 manage.py test --fail-fast

然而,在 heroku CI 上运行这些时,当 django 系统尝试渲染设置时,我得到一个无限循环:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner

    response = get_response(request)

  File "/app/investorsuite/middleware.py", line 11, in middleware

    response = get_response(request)

  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 36, in inner

    response = response_for_exception(request, exc)

  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 95, in response_for_exception

    exc_info=sys.exc_info(),

  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/log.py", line 228, in log_response

    exc_info=exc_info,

  File "/app/.heroku/python/lib/python3.6/logging/__init__.py", line 1337, in error

    self._log(ERROR, msg, args, **kwargs)

  File "/app/.heroku/python/lib/python3.6/logging/__init__.py", line 1444, in _log

    self.handle(record)

  File "/app/.heroku/python/lib/python3.6/logging/__init__.py", line 1454, in handle

    self.callHandlers(record)

  File "/app/.heroku/python/lib/python3.6/logging/__init__.py", line 1516, in callHandlers

    hdlr.handle(record)

  File "/app/.heroku/python/lib/python3.6/logging/__init__.py", line 865, in handle

    self.emit(record)

  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/log.py", line 120, in emit

    message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text())

  File "/app/.heroku/python/lib/python3.6/site-packages/django/views/debug.py", line 340, in get_traceback_text

    c = Context(self.get_traceback_data(), autoescape=False, use_l10n=False)

  File "/app/.heroku/python/lib/python3.6/site-packages/django/views/debug.py", line 306, in get_traceback_data

    'settings': get_safe_settings(),

  File "/app/.heroku/python/lib/python3.6/site-packages/django/views/debug.py", line 80, in get_safe_settings

    settings_dict[k] = cleanse_setting(k, getattr(settings, k))

  File "/app/.heroku/python/lib/python3.6/site-packages/django/views/debug.py", line 58, in cleanse_setting

    cleansed = {k: cleanse_setting(k, v) for k, v in value.items()}

  File "/app/.heroku/python/lib/python3.6/site-packages/django/views/debug.py", line 58, in <dictcomp>

    cleansed = {k: cleanse_setting(k, v) for k, v in value.items()}

  File "/app/.heroku/python/lib/python3.6/site-packages/django/views/debug.py", line 58, in cleanse_setting

    cleansed = {k: cleanse_setting(k, v) for k, v in value.items()}
.......

RecursionError: maximum recursion depth exceeded

而且我不确定该错误背后的原因是什么。我已经查找了可能有问题的字典(cleanse_setting 递归地输入字典),但我似乎在我的设置文件中找不到任何字典:

import os
import sentry_sdk
from django.contrib.messages import constants as messages
from sentry_sdk.integrations.django import \
    DjangoIntegration

from decimal import Decimal

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG_ENV = os.getenv('DEBUG_MODE', '1')
DEBUG = DEBUG_ENV.lower() == 'true' or DEBUG_ENV == '1'

SITE_NAME = os.getenv("SITE_NAME", '********')

DOMAIN = os.getenv('DOMAIN', '********')

ALLOWED_HOSTS = [
    DOMAIN,
    "." + DOMAIN,  # subdomains
]

# Application definition

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

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

MESSAGE_TAGS = {
    messages.DEBUG: 'alert-info',
    messages.INFO: 'alert-info',
    messages.SUCCESS: 'alert-success',
    messages.WARNING: 'alert-warning',
    messages.ERROR: 'alert-danger',
}

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 = '******'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, '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',
            ],
        },
    }
]

PROJECT_DIR = os.path.dirname(__file__)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

WSGI_APPLICATION = '********'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
        },
        'NAME': '********',
        'USER': '********',
        'PASSWORD':'********',
        'HOST': 'localhost',
        'PORT': '3306',
       'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

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

##
AUTH_USER_MODEL = "accounts.User"

LOGIN_URL = "/"
LOGOUT_REDIRECT_URL = "/"
# media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
# emailing

EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = '********'
EMAIL_HOST_PASSWORD = '********'
EMAIL_USE_TLS = True

# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

MANAGEMENT_FEE = Decimal("0.02")
PERFORMANCE_FEE = Decimal("0.2")

try:
    from settings_local import *
except ImportError:
    pass

if DEBUG and 'EMAIL_BACKEND' not in vars():
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

SENTRY_DOMAIN = '********'

# TODO make environment a bit smarter based on environment


if not DEBUG:
    import socket

    SENTRY_IO_NAME = os.getenv('SENTRY_IO_NAME', socket.gethostname())

    sentry_sdk.init(
        SENTRY_DOMAIN,
        integrations=[DjangoIntegration()],
        environment=SENTRY_IO_NAME
    )

# Configure Django App for Heroku.
try:
    import django_heroku

    if os.getenv('JAWSDB_URL', '') is not '':
        os.environ['DATABASE_URL'] = os.getenv('JAWSDB_URL', '')
    django_heroku.settings(locals())

    # following line because of https://github.com/kennethreitz/dj-database-url/issues/107
    DATABASES['default']['init_command'] = "SET sql_mode='STRICT_TRANS_TABLES'"
    try:
        del DATABASES['default']['OPTIONS']['sslmode']
    except:
        pass

except ModuleNotFoundError:
    print("Heroku not in use.")

利用 Django2.2.1 和 python 3.6.8

编辑:很明显,设置中的 DATABASE 值是由 heroku 出于某种原因递归定义的?:

>>> settings.DATABASES['default']['TEST']['TEST']
{'NAME': '*****', 'USER': '*****', 'PASSWORD': '*****', 'HOST': '******', 'PORT': 3306, 'CONN_MAX_AGE': 600, 'OPTIONS': {}, 'ENGINE': 'django.db.backends.mysql', 'TEST': {...}, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", 'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'TIME_ZONE': None, 'CHARSET': None, 'COLLATION': None, 'MIRROR': None}

这并没有真正让我感到困惑

EDIT2:我试过清理文件并测试不同的东西,感觉很迷茫。我真的不知道从这里去哪里。我应该在 django_heroku 的 git repo 上报告错误吗?可能只是我做错了什么,但我真的不知道该去哪里。我将尝试创建一个 django 应用程序的最小实现,并尝试在接下来的几天(最坏的情况下)重现该错误。

4

0 回答 0