【问题标题】:Django on heroku DEBUG settingsDjango 上的 heroku 调试设置
【发布时间】:2021-02-13 20:13:07
【问题描述】:

我对 django 的设置有点困惑。

我在本地的 settings.py 文件中有类似的设置:

"""
Django settings for ebportfolio project.

Generated by 'django-admin startproject' using Django 3.1.6.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import environ

env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
environ.Env.read_env()
DEBUG = env('DEBUG')

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = environ.Path(__file__)-1
# BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!

ALLOWED_HOSTS = ['127.0.0.1', 'my heroku address']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cloudinary',
    'cloudinary_storage',
    'django_summernote',
    'projects',
    'blog',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'ebportfolio.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['ebportfolio/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 = 'ebportfolio.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
    # read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
    'default': env.db(),
    # read os.environ['SQLITE_URL']
}

# Password validation
# https://docs.djangoproject.com/en/3.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',
    },
]

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/London'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

public_root = BASE_DIR.path('')
MEDIA_ROOT = public_root('media')
MEDIA_URL = env.str('MEDIA_URL', default='media/')
STATIC_ROOT = public_root('staticfiles')
STATIC_URL = env.str('STATIC_URL', default='staticfiles/')

X_FRAME_OPTIONS = 'SAMEORIGIN'
# Configure Django App for Heroku.
DJANGO_SETTINGS_MODULE ='ebportfolio.settings'
import django_heroku

django_heroku.settings(locals())

在我的 .env 文件 DEBUG=True 中设置 DEBUG=on 时,图像会在线和本地显示。当我在 .env 中切换 DEBUG=off 时,图像不会显示在本地运行。有人可以向我解释为什么吗? DEBUG 是否会影响文件的显示位置?

在撰写帖子时进行编辑 - 我将允许主机中的 heroku URL 更改为包含 https://,现在图像显示。我应该查看哪些文档才能理解这一点?

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    因为off 被视为字符串,非空字符串仍然是真实的

    您需要从 env 文件中完全删除 DEBUG 才能将其设置为 false(或虚假值,例如 DEBUG=0

    【讨论】:

    • 本地:图像在 Debug True、False、on、off,.env 中没有 DEBUG 在 heroku 上:在 env 变量中 DEBUG 设置为 True 的图像。当我将其更改为 False 时,图像消失。
    • 其实,现在想想——我想是因为我是直接用项目上传文件的吧?我在某处读到您想从 s3 或其他外部来源加载媒体?我只是尝试了一堆 DEBUG 设置,无论我在环境中设置什么,图像都刚刚开始显示
    • 好吧,我真的不知道为什么,但似乎无论如何都会出现图像。调试 0 也是如此。如果将 debug 设置为 true 或 false,实际的区别是什么?它只查看允许的主机吗?
    • 但我认为它与调试变量无关。我刚才所做的是 - 1. 推送到 heroku 2. 运行 collectstatic 3. 网站工作,但没有显示图像 4. 将环境变量 DEBUG 从 0 更改为 True - 图像开始显示 5. 将 DEBUG True 更改为 0 和图像还可以。
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2015-11-12
    • 2012-10-11
    • 2014-06-30
    • 2020-12-17
    相关资源
    最近更新 更多