python - django 应用程序在 heroku TypeError : expected str, bytes 或 os.PathLike 对象(而不是函数)上崩溃

标签 python django heroku

我的 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 上发表的第一篇文章,因此如果我需要提供更多信息,请告诉我。

最佳答案

所以我昨天就明白了这一点。该错误是由于 settings.py 文件中未正确配置 whitenoise 中间件造成的。

而不是

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

我应该有

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

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

关于python - django 应用程序在 heroku TypeError : expected str, bytes 或 os.PathLike 对象(而不是函数)上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54772174/

相关文章:

python - 只有在成功创建其他对象时才创建对象

python - 字符串到列表的转换

python - Python 中的 C 数

python - 需要正则表达式的帮助来获取强制值之后的任何内容

django - 如果元素位于查询中的第一个,则添加 html 代码 - Django

javascript - 在 Jquery 中定义 Django 上下文变量给我错误?

ruby-on-rails - 使用 Heroku 和 PostgreSQL,我还需要 Nginx 吗?

node.js - "Fetch failed loading"- 提取在 Postman 中有效,但在 Chrome 和 Safari 中失败

django admin - 导出为 csv

ruby-on-rails - 在 heroku 上找不到字体 (404)