Skip to main content
 首页 » 编程设计

python之django 应用程序在 heroku TypeError : expected str, 字节或 os.PathLike 对象上崩溃,无法运行

2024年09月03日38oomusou

我的 django 应用无法在 heroku 上运行,我不知道为什么。

我从 heroku 得到以下错误代码:

2019-02-19T17:36:16.022570+00:00 app[web.1]: 
  TypeError: expected str, bytes or os.PathLike object, not function 

然后当我导航到该站点时,我得到了这个:

2019-02-19T17:37:21.630743+00:00 heroku[router]: at=error code=H10  
  desc="App crashed" method=GET path="/" host=full-stack-frameworks-django.herokuapp.com 
  request_id=9d5b7ef6-790f-4584-921a-d1392842c93a fwd="109.78.21.112"  
  dyno= connect= service= status=503 bytes= protocol=https 

这是我的settings.py文件

import os 
# from dotenv import load_dotenv 
import psycopg2 
import dj_database_url 
 
DATABASE_URL = os.environ['DATABASE_URL'] 
conn = psycopg2.connect(DATABASE_URL, sslmode='require') 
 
# WHITENOISE_USE_FINDERS = True 
 
# dotenv_path = os.path.join(os.path.dirname(__file__), '.env') 
# load_dotenv(dotenv_path) 
 
# 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.1/howto/deployment/checklist/ 
 
# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = os.environ.get('SECRET_KEY') 
 
# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = False 
 
ALLOWED_HOSTS = [] 
 
 
# Application definition 
 
INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django_forms_bootstrap', 
    'home', 
    'accounts', 
    'blogposts', 
    'tickets', 
    'payments', 
] 
 
MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.storage.CompressedManifestStaticFilesStorage', 
    '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 = 'IssueTracker.urls' 
 
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', 
                'django.template.context_processors.media', 
            ], 
        }, 
    }, 
] 
 
WSGI_APPLICATION = 'IssueTracker.wsgi.application' 
 
 
# Database 
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases 
 
DATABASES={} 
 
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True) 
 
# DATABASES = { 
#     'default': { 
#         'ENGINE': 'django.db.backends.sqlite3', 
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
#     } 
# } 
 
 
# Password validation 
# https://docs.djangoproject.com/en/2.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/2.1/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/2.1/howto/static-files/ 
 
STATIC_URL = '/static/' 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static') 
] 
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root') 
 
 
MEDIA_URL = '/media/' 
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 
 
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage' 
 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_HOST_USER = os.environ.get('EMAIL_ADDRESS') 
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD') 
EMAIL_PORT = 587 
 
# TEST KEYS 
STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY') 
# 
STRIPE_PUBLISHABLE_KEY = os.environ.get('STRIPE_PUBLISHABLE_KEY') 
# 

我有一个我认为是正确的 procfile:

web: gunicorn IssueTracker.wsgi:application 

这是我在 StackOverflow 上的第一篇文章,所以如果我需要提供更多信息,请告诉我。

请您参考如下方法:

所以我昨天想通了。该错误是由于 whitenoise 中间件未在 settings.py 文件中正确配置。

代替

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.storage.CompressedManifestStaticFilesStorage', 
.... 
.... 

我应该有

MIDDLEWARE = [ 
        'django.middleware.security.SecurityMiddleware', 
        'whitenoise.middleware.WhiteNoiseMiddleware', 
    .... 
    .... 

CompressedManifestStaticFilesStorage实际上应该应用于 settings.py 中的 STSTICFILES_STORAGE 变量,如果您希望对静态文件应用压缩。