通过http提供的静态资源不会通过https提供

时间:2018-08-01 13:06:43

标签: django https

我用Django编写了一个应用程序,该应用程序提供使用Webpack构建的文件。该应用程序可以在http上正常运行,但是在https上却出现错误,我似乎找不到解决方法。

将调试设置为True时(例如)

GET https://localhost:8000/dist/bundles/css/main.f57231b6.css net::ERR_ABORTED 404 (Not Found)

将debug设置为false我得到

GET https://localhost:8000/dist/bundles/css/main.f57231b6.css net::ERR_ABORTED 500 (Internal Server Error)

用于CSS和JavaScript。服务器上的回溯看起来像这样:

File "/Applications/anaconda3/lib/python3.6/wsgiref/handlers.py", line 137, in run self.result = application(self.environ, self.start_response) File "/Users/home/docs/Projects/Apprentice_Project/aws_env/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__ return super().__call__(environ, start_response) File "/Users/home/docs/Projects/Apprentice_Project/aws_env/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 146, in __call__ response = self.get_response(request) File "/Users/home/docs/Projects/Apprentice_Project/aws_env/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 62, in get_response return super().get_response(request) File "/Users/home/docs/Projects/Apprentice_Project/aws_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 81, in get_response response = self._middleware_chain(request) TypeError: 'NoneType' object is not callable [01/Aug/2018 05:48:59] "GET /dist/bundles/css/main.f57231b6.css HTTP/1.1" 500 59

谁能告诉我为什么我的https出现NoneType错误?

settings.py:

"""
Django settings for prentice project.

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

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

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

import os
import environ

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# defining which variables django-environ should look for
env = environ.Env(
    SECRET_KEY=str,
    DB_HOST=(str, '127.0.0.1'),
    DB_NAME=str,
    DB_USER=str,
    DB_PASSWORD=str,
    DB_PORT=int
)

#Read environment variables
environ.Env.read_env(os.path.join(BASE_DIR, '.env.prod'))


# 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!
# Set to 'True' for dev environment
DEBUG = True

ALLOWED_HOSTS = ['localhost', 'prentice.us-east-1.elasticbeanstalk.com']


# Application definition

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

    # 3rd party apps
    'webpack_loader',
    'knox',
    'sslserver',

    # Custom apps
    ...

]

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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'configuration.urls'


WSGI_APPLICATION = 'configuration.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
if 'RDS_DB_NAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': env('DB_NAME'),
            'USER': env('DB_USER'),
            'PASSWORD': env('DB_PASSWORD'),
            'HOST': env('DB_HOST'),
            'PORT': env('DB_PORT'),
        }
    }
#Substitute custom User model

AUTH_USER_MODEL = 'auth_api.User'

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

# Rest framework settings

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',),
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Dawson'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/dist/'
STATIC_ROOT = 'dist'

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

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "dist","bundles"),
]

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

WEBPACK_LOADER = {
    'DEFAULT': {
            'BUNDLE_DIR_NAME': 'bundles/',
            'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.prod.json'),
        }
}

1 个答案:

答案 0 :(得分:0)

我认为: 关闭调试功能后,Django将不再为您处理静态文件-您的生产Web服务器(Apache或django-whitenoise)应注意这一点 希望对您有所帮助:)