【问题标题】:Why it doesn't set DEBUG = True?为什么它没有设置 DEBUG = True?
【发布时间】:2021-06-29 13:29:00
【问题描述】:

我正在一个带有 venv 的 Django 项目中工作。我的问题是下一个问题:我在下面的代码中配置了 DEBUG = TRUE,但似乎在 django 启动时未加载设置并启动此错误:You must set settings.ALLOWED_HOSTS if DEBUG is False.

我认为DEBUG在虚拟环境中发生了变化,因为我调试了我的代码,并且在启动服务器之前发现DEBUG为false

import django_heroku
import os

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

DEBUG = True

ALLOWED_HOSTS = []

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

PRIORITY_THIRD_PARTY_PASS = [
    'django_admin_env_notice',
]

THIRD_PARTY_APPS = [
    'rest_framework',
    'rest_framework.authtoken',
    'django_filters',
    'django_otp',
    'django_otp.plugins.otp_totp',
    'drf_yasg',
]

LOCAL_APPS = [
    'finalusers',
    'reports',
]

INSTALLED_APPS = PRIORITY_THIRD_PARTY_PASS + DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS


AUTH_USER_MODEL = 'users.CustomUser'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
}

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',
    'django_otp.middleware.OTPMiddleware',
]

ROOT_URLCONF = 'circulo.urls'

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',
                'django_admin_env_notice.context_processors.from_settings',
            ],
            'debug': DEBUG,
        },
    },
]

WSGI_APPLICATION = 'circulo.wsgi.application'

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 = 'America/Mexico_City'

USE_I18N = True

USE_L10N = True

USE_TZ = True

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

django_heroku.settings(locals())

LOGIN_URL = '/login'

ENVIRONMENT_NAME = "ENVIRONMENT NOT SET"
ENVIRONMENT_COLOR = "#000000"

OTP_TOTP_ISSUER = '{{ project_name }}'

if USE_TZ:

    CELERY_TIMEZONE = TIME_ZONE

CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', 'redis://localhost:6379')result_serializer


CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_ENABLE_UTC = True
redbeat_redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379/1')

【问题讨论】:

  • 是否被 django_heroku.settings 覆盖?

标签: python django python-venv


【解决方案1】:

嗯,我发现了问题:

我忘记在manage.py中的配置位置重命名名称,所以venv无法找到配置,然后他相应地设置了默认配置(DEBUG = False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2020-11-14
    • 2015-02-08
    • 2011-05-26
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多