簡體   English   中英

Heroku 當 Debug 為 False 且 collectstatic 不起作用時出現內部服務器錯誤 - Django

[英]Heroku Internal Server Error when Debug is False and collectstatic dosen't work - Django

我部署了一個應用程序到 Heroku,它使用 Postgres。 任何時候我將 Debug 設置為 True,它都可以正常工作。 當我將它設置回 False 時,它會給出錯誤 500,內部服務器錯誤。 我不知道為什么要這樣做。 它與靜態文件有關嗎? 我正在使用 whitenoise 來提供 static 個文件。

每當我運行heroku run python manage.py collectstatic時,它都會給我

Running python manage.py collectstatic on djangotestapp... up, run.9614 (Free)

300 static files copied to '/app/staticfiles', 758 post-processed.

當我運行heroku run bash時,我發現沒有staticfiles文件夾。 它究竟將 static 文件復制到哪里? 這是導致錯誤 500 的原因嗎? 我該如何解決?

設置.py


import os
import dj_database_url
from decouple import config, Csv

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', cast=bool)

SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_SECONDS = 200
SECURE_HSTS_PRELOAD = True

ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast =Csv())

# Application definition

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

    # Third-party
    'allauth',
    'allauth.account',
    'crispy_forms',
    'widget_tweaks',
    'debug_toolbar',
    'phonenumber_field',
    'django_datatables_view',
    
    'whitenoise.runserver_nostatic',     

    # Local
    'users',
    'pages',
    'attendance',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # WHITENOISE
    'django.middleware.common.CommonMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'djangotestapp_project.urls'

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

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('DB_NAME'),
        'USER':  config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST':  config('DB_HOST'),
        'PORT': '5432',
    },  
}

prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)

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

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

#STATICFILES_FINDERS = [    
#    'django.contrib.staticfiles.finders.FileSystemFinder',
#    'django.contrib.staticfiles.finders.AppDirectoriesFinder',    
#]


#STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
#
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

更新我運行heroku logs -tail -a djangotestapp來查看發生的錯誤,這就是它給我的。

2020-08-30T20:23:20.090560+00:00 app[web.1]: raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
2020-08-30T20:23:20.090560+00:00 app[web.1]: ValueError: Missing staticfiles manifest entry for 'css/font-rules-roboto.css'

文件font-rules-roboto.css使我能夠在沒有任何 Google 字體 API 鏈接的情況下使用 roboto 字體。

我運行python manage.py collectstatic ,但它什么也沒做。

我認為您的問題與您的ALLOWED_HOSTS有關。 當您關閉 Django 設置的調試模式時,您必須將項目主機 IP 或域添加到ALLOWED_HOSTS 所以我認為將您的主機服務器的 IP 添加到您的.env文件中( ALLOWED_HOSTS = localhost, ..., IP.OF.YOUR.HOST )將解決您的問題。

謝謝@Roham 的幫助。 我通過添加步驟解決了它。

在我評論STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'之后,網站開始工作,但 static 文件沒有加載。

然后我將這一行添加到設置中, WHITENOISE_USE_FINDERS = True 它允許您的應用程序運行而無需收集靜態文件。

在我這樣做之后,所有 static 文件都加載沒有任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM