debugging Django DEBUG False无法在生产环境中工作

bbmckpt7  于 2022-12-13  发布在  Go
关注(0)|答案(1)|浏览(187)

I am new to Django and Python world, I am currently working on an Dajngo site, which is hosted on a Ubuntu20.04 VM. Upon hosting the site to the production, I noticed that, although the DEBUG is set to False in the prodiuction settings. I still see the Django DEBUG erros as if the DEBUG is set to True .
When I run python manage.py runserver command on the VM I get the below error.

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

Folder structure

.
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-310.pyc
│   ├── urls.cpython-310.pyc
│   ├── views.cpython-310.pyc
│   └── wsgi.cpython-310.pyc
├── settings
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-310.pyc
│   │   ├── base.cpython-310.pyc
│   │   ├── local.cpython-310.pyc
│   │   └── production.cpython-310.pyc
│   ├── base.py
│   ├── local.py
│   └── production.py

base.py (Common settings)

import os
from os.path import dirname, join
import posixpath
from posixpath import join
from pathlib import Path


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '393266f4-9cc7-4cfe-96a8-2f2809e53e32'

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

#ALLOWED_HOSTS = []

# Application references
INSTALLED_APPS = [
    # Add your apps here to enable them
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'AssayBlog',
    'AssayCMS',
    'ckeditor',
    'ckeditor_uploader',
]

# Middleware framework
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 = 'AssaySite.urls'

# Template configuration

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

WSGI_APPLICATION = 'AssaySite.wsgi.application'

# Database

#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.sqlite3',
#        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#    }
#}


# Password validation

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

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Default primary key field type

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# HTTPS SETTINGS
SESSIONS_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True #False
SECURE_SSL_REGIRECT = True

# HSTS SETTINGS
SECURE_HSTS_SECONDS = 31536000  # 1 YEAR
SECURE_HSTS_PRELOAD = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True

CKEDITOR_UPLOAD_PATH = "uploads/"

production.py

from AssaySite.settings.base import *

DEBUG = False

ALLOWED_HOSTS = [
    'www.exampl.com',
    'example.com',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'abssaysitedb',
        'USER': 'admingres',
        'PASSWORD': 'password',
        'HOST': '192.168.1.201',
        'PORT': '8686',
    }
}

local.py

from AssaySite.settings.base import *

DEBUG = False

ALLOWED_HOSTS = [
    '127.0.0.1',
    'localhost'
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'admin',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

I have carefully reviewed the settings code to see if the DEBUG is True in any part of my code but everything looks good. Please help

tkqqtvp1

tkqqtvp11#

You need to ALLOWED_HOSTS variable in settings.py.

ALLOWED_HOSTS = ['your_domain.com','www.your_domain.com']

or, this will allow all the domain which is not recommended

ALLOWED_HOSTS = ['*']

相关问题