【问题标题】:Django project's settings file DEBUG = False [closed]Django项目的设置文件DEBUG = False [关闭]
【发布时间】:2019-09-03 19:17:42
【问题描述】:

Django 项目设置文件,DEBUG= False

主要思想是隐藏项目的一些附加重要信息,如环境变量信息、托管信息、静态文件信息等等。

如果我们在生产中使用 DEBUG= True,那么我们可以看到很多项目信息使用浏览器,例如有时我们使用 MVC 模式创建应用程序,如果我们得到错误然后我们可以看到使用浏览器的最大错误。

让我们看看这里设置我的文件 DEBUG 部分并设置 DEBUG=True

"""
Django settings for school project.

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

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

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

from pathlib import Path

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-=$c*dgnsr0*^vk9%l=9&0*d(_b)ya&1gk1psb#8zt=6wnli1d9'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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

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 = 'school.urls'

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


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

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


# Password validation
# https://docs.djangoproject.com/en/3.2/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.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


**Start the server use command: python manage.py runserver**

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 17, 2021 - 09:37:32
Django version 3.2.4, using settings 'school.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

启动服务器后,如果我们在没有 /admin 的情况下路由任何类型的路由,则会出现错误,我们可以从浏览器中看到此错误

在这里查看错误

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/hhh
Using the URLconf defined in school.urls, Django tried these URL patterns, in this order:

admin/
The current path, hhh, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

像这个错误一样,我们可以从浏览器中看到很多错误,这对项目非常有害

**所以解决方案是:** 我们必须写 DEBUG=False 并且没有人可以看到这种错误或其他项目的东西

如果我们使用 DEBUG= False 那么我们将面临项目中的一些其他问题,例如修复错误,设置一些其他信息但是如果我们清楚地了解Django settingsfile 那么一切都会非常在我们面前清晰。

谢谢@所有人

【问题讨论】:

    标签: python python-3.x django django-rest-framework


    【解决方案1】:

    在设置中检查INSTALLED_APPS

    ...
    'django.contrib.staticfiles',
    ...
    

    还有:

    STATIC_URL = '/static/'
    

    还有:

    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    

    如果这没有帮助,请显示完整的 settings.py 文件。

    【讨论】:

    • Rudnev 感谢您的好意。我已经在使用这个系统,一切正常,但图像不工作。还有什么建议吗?
    • 请显示完整的settings.py
    • import django_heroku import os import environ env = environ.Env( DEBUG=(bool, True), ALLOWED_HOSTS=(list), ) envfile_path = os.path.join(BASE_DIR, '.env') environ.Env.read_env(envfile_path) DEBUG = env('DEBUG') SECRET_KEY = env('SECRET_KEY') ALLOWED_HOSTS = env('ALLOWED_HOSTS')
    猜你喜欢
    • 2021-11-19
    • 2019-01-05
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多