Django应用程序仅适用于Debug = True Heroku

时间:2018-03-13 21:18:05

标签: django heroku

我以为我解决了这个问题,但我猜不是。我在Heroku上获得了我的Django应用程序,它与DEBUG = True完美配合,但不适用于DEBUG = False

更新的问题

现在我正在与这个错误作斗争:

2018-03-14T18:42:08.812921 + 00:00 heroku [router]:at = error code = H12 desc =" Request timeout" method = GET path =" /" host = www.powertranspro.com request_id = f75a1cec-d303-4f5c-8eee-ddf605b7c326 fwd =" 71.38.218.24" dyno = web.1 connect = 0ms service = 30001ms status = 503 bytes = 0 protocol = http

有人会介意查看我的设置文件,看看我哪里出错了。

答案 我和whitenoise有冲突。所以我经历并删除了与whitenoise有关的一切,并将我的静态文件移动到了亚马逊S3。我正在使用此帖子上的正确代码更新我的代码,以防其他人遇到类似问题。

首先是我的文件结构:

enter image description here

base.py

# settings/base.py
import os
from django.utils import timezone
from decouple import config

DEBUG = config('DEBUG', cast=bool)
GOOGLE_MAPS_API_KEY = config('GOOGLE_MAPS_API_KEY')
EASY_MAPS_GOOGLE_MAPS_API_KEY = config('GOOGLE_MAPS_API_KEY')

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = 'powertrans-pro-bucket'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'config/static'),
]

STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'config.settings.storage_backends.MediaStorage'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR],
        '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',
                'apps.todos.context_processors.todos_processor',
                'apps.freight_projects.context_processors.freight_projects_processor',
            ],
            'debug': DEBUG,
        },
    },
]

INSTALLED_APPS = [
    'dal',
    'dal_select2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.sites',
    'django.contrib.staticfiles',
    'storages',
    'django_misaka',
    'bootstrap3',
    'localflavor',
    'easy_pdf',
    'django_google_maps',
    'easy_maps',
    'widget_tweaks',
    # django-allauth
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.linkedin',
    # end django all-auth
    # my created apps
    'apps.accounts',
    'apps.user_dashboard',
    'apps.customer_dashboard',
    'apps.freight_projects',
    'apps.todos',
    'apps.locations',
    'apps.loads',
    'apps.project_template_tags',
    'apps.quotes',
    # end my created 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',
]

ROOT_URLCONF = 'config.urls'

WSGI_APPLICATION = 'config.wsgi.application'

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

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
)
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

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

# Change 'default' database configuration with $DATABASE_URL.
#DATABASES['default'].update(dj_database_url.config(conn_max_age=500))

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers


SITE_ID = 1
LOGIN_REDIRECT_URL = 'login_success'
LOGOUT_REDIRECT_URL = 'logout_confirmation'

#MAILGUN SETTINGS
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_PORT = config('EMAIL_PORT', cast=int)
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = config('EMAIL_USE_TLS', cast=bool)

AUTH_USER_MODEL = 'accounts.User'

production.py

# settings/production.py
from .base import *
import os
from decouple import config
import dj_database_url

DEBUG = config('DEBUG', cast=bool)

GOOGLE_MAPS_API_KEY = config('GOOGLE_MAPS_API_KEY')

SECRET_KEY = config('SECRET_KEY')

ALLOWED_HOSTS = ['shielded-tundra-23748.herokuapp.com', '.powertranspro.com']

DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}

DATABASES['default']['CONN_MAX_AGE'] = 500

HEROKU LOGS

2018-03-13T21:53:23.232139+00:00 app[web.1]: [2018-03-13 21:53:23 +0000] [4] [INFO] Handling signal: term
2018-03-13T21:53:23.234260+00:00 app[web.1]: [2018-03-13 21:53:23 +0000] [9] [INFO] Worker exiting (pid: 9)
2018-03-13T21:53:23.236062+00:00 app[web.1]: [2018-03-13 21:53:23 +0000] [8] [INFO] Worker exiting (pid: 8)
2018-03-13T21:53:23.543646+00:00 heroku[beat.1]: Stopping all processes with SIGTERM
2018-03-13T21:53:23.575710+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
2018-03-13T21:53:23.591368+00:00 app[worker.1]: 
2018-03-13T21:53:23.591399+00:00 app[worker.1]: worker: Warm shutdown (MainProcess)
2018-03-13T21:53:23.749154+00:00 app[web.1]: [2018-03-13 21:53:23 +0000] [4] [INFO] Shutting down: Master
2018-03-13T21:53:24.012876+00:00 heroku[beat.1]: Process exited with status 0
2018-03-13T21:53:24.023046+00:00 heroku[web.1]: Process exited with status 0
2018-03-13T21:53:25.509937+00:00 heroku[worker.1]: Process exited with status 0
2018-03-13T21:53:43.386916+00:00 heroku[worker.1]: Starting process with command `celery -A config worker --beat`
2018-03-13T21:53:44.117616+00:00 heroku[worker.1]: State changed from starting to up
2018-03-13T21:53:45.218810+00:00 heroku[beat.1]: Starting process with command `celery -A config beat -S django`
2018-03-13T21:53:45.731767+00:00 app[worker.1]: ['/app/config/static']
2018-03-13T21:53:46.053233+00:00 heroku[beat.1]: State changed from starting to up
2018-03-13T21:53:46.582872+00:00 heroku[web.1]: Starting process with command `gunicorn config.wsgi --log-file -`
2018-03-13T21:53:47.349183+00:00 app[worker.1]:  
2018-03-13T21:53:47.349228+00:00 app[worker.1]:  -------------- celery@b0aaf05c-7b04-4327-9e0c-10c3fe7f2965 v4.1.0 (latentcall)
2018-03-13T21:53:47.349231+00:00 app[worker.1]: ---- **** ----- 
2018-03-13T21:53:47.349234+00:00 app[worker.1]: --- * ***  * -- Linux-4.4.0-1012-aws-x86_64-with-debian-stretch-sid 2018-03-13 21:53:47
2018-03-13T21:53:47.349236+00:00 app[worker.1]: -- * - **** --- 
2018-03-13T21:53:47.349237+00:00 app[worker.1]: - ** ---------- [config]
2018-03-13T21:53:47.349240+00:00 app[worker.1]: - ** ---------- .> app:         POTRTMS:0x7f638769d7b8
2018-03-13T21:53:47.349241+00:00 app[worker.1]: - ** ---------- .> transport:   redis://h:**@ec2-34-239-77-182.compute-1.amazonaws.com:53459//
2018-03-13T21:53:47.349243+00:00 app[worker.1]: - ** ---------- .> results:     redis://h:**@ec2-34-239-77-182.compute-1.amazonaws.com:53459/
2018-03-13T21:53:47.349246+00:00 app[worker.1]: - *** --- * --- .> concurrency: 8 (prefork)
2018-03-13T21:53:47.349247+00:00 app[worker.1]: -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
2018-03-13T21:53:47.349249+00:00 app[worker.1]: --- ***** ----- 
2018-03-13T21:53:47.349250+00:00 app[worker.1]:  -------------- [queues]
2018-03-13T21:53:47.349252+00:00 app[worker.1]:                 .> celery           exchange=celery(direct) key=celery
2018-03-13T21:53:47.349254+00:00 app[worker.1]:                 
2018-03-13T21:53:47.349262+00:00 app[worker.1]: 
2018-03-13T21:53:47.860250+00:00 app[beat.1]: ['/app/config/static']
2018-03-13T21:53:47.881589+00:00 app[beat.1]: celery beat v4.1.0 (latentcall) is starting.
2018-03-13T21:53:49.678712+00:00 app[beat.1]: __    -    ... __   -        _
2018-03-13T21:53:49.678762+00:00 app[beat.1]: LocalTime -> 2018-03-13 21:53:49
2018-03-13T21:53:49.678765+00:00 app[beat.1]: Configuration ->
2018-03-13T21:53:49.678767+00:00 app[beat.1]:     . broker -> redis://h:**@ec2-34-239-77-182.compute-1.amazonaws.com:53459//
2018-03-13T21:53:49.678769+00:00 app[beat.1]:     . loader -> celery.loaders.app.AppLoader
2018-03-13T21:53:49.678771+00:00 app[beat.1]:     . scheduler -> django_celery_beat.schedulers.DatabaseScheduler
2018-03-13T21:53:49.678773+00:00 app[beat.1]: 
2018-03-13T21:53:49.678774+00:00 app[beat.1]:     . logfile -> [stderr]@%WARNING
2018-03-13T21:53:49.678776+00:00 app[beat.1]:     . maxinterval -> 5.00 seconds (5s)
2018-03-13T21:53:49.884036+00:00 app[web.1]: [2018-03-13 21:53:49 +0000] [4] [INFO] Starting gunicorn 19.7.1
2018-03-13T21:53:49.884609+00:00 app[web.1]: [2018-03-13 21:53:49 +0000] [4] [INFO] Listening at: http://0.0.0.0:15685 (4)
2018-03-13T21:53:49.884728+00:00 app[web.1]: [2018-03-13 21:53:49 +0000] [4] [INFO] Using worker: sync
2018-03-13T21:53:49.889062+00:00 app[web.1]: [2018-03-13 21:53:49 +0000] [8] [INFO] Booting worker with pid: 8
2018-03-13T21:53:49.892636+00:00 app[web.1]: [2018-03-13 21:53:49 +0000] [9] [INFO] Booting worker with pid: 9
2018-03-13T21:53:50.816149+00:00 heroku[web.1]: State changed from starting to up
2018-03-13T21:53:51.040875+00:00 app[web.1]: ['/app/config/static']
2018-03-13T21:53:51.180555+00:00 app[web.1]: ['/app/config/static']
2018-03-13T21:53:25+00:00 app[heroku-redis]: source=REDIS sample#active-connections=1 sample#load-avg-1m=0.08 sample#load-avg-5m=0.13 sample#load-avg-15m=0.125 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15664184kB sample#memory-free=12008784kB sample#memory-cached=1496784kB sample#memory-redis=312336bytes sample#hit-rate=0.41381 sample#evicted-keys=0
2018-03-13T21:54:20.036317+00:00 app[web.1]: [2018-03-13 21:54:20 +0000] [4] [CRITICAL] WORKER TIMEOUT (pid:8)
2018-03-13T21:54:20.036666+00:00 app[web.1]: [2018-03-13 21:54:20 +0000] [4] [CRITICAL] WORKER TIMEOUT (pid:9)
2018-03-13T21:54:20.037926+00:00 app[web.1]: [2018-03-13 21:54:20 +0000] [8] [INFO] Worker exiting (pid: 8)
2018-03-13T21:54:20.038168+00:00 app[web.1]: [2018-03-13 21:54:20 +0000] [9] [INFO] Worker exiting (pid: 9)
2018-03-13T21:54:20.337323+00:00 app[web.1]: [2018-03-13 21:54:20 +0000] [12] [INFO] Booting worker with pid: 12
2018-03-13T21:54:20.438551+00:00 app[web.1]: [2018-03-13 21:54:20 +0000] [14] [INFO] Booting worker with pid: 14
2018-03-13T21:54:20.966862+00:00 app[web.1]: ['/app/config/static']
2018-03-13T21:54:21.114239+00:00 app[web.1]: ['/app/config/static']
2018-03-13T21:54:23.414661+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/loads/load_list/filter/" host=www.powertranspro.com request_id=912215ee-893e-431a-8849-a6cb0195ade8 fwd="71.38.218.24" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=http

回溯

Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  156.                 response = response.render()

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/response.py" in render
  106.             self.content = self.rendered_content

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

Exception Type: ValueError at /loads/load_list/filter/
Exception Value: Missing staticfiles manifest entry for 'vendors/bootstrap/dist/css/bootstrap.min.css'
Request information:

2 个答案:

答案 0 :(得分:0)

来自Whitenoise Docs:

  

如果您遇到WhiteNoise存储后端问题,请执行此操作   可能是因为底层的Django存储引擎。这个   是因为WhiteNoise只在Django的存储周围添加了一个薄的包装器   添加压缩支持,因为压缩代码非常   简单一般不会引起问题。

     

最常见的问题是有引用的CSS文件   其他文件(通常是图像或字体)在那里不存在   指定的路径。当Django尝试重写这些引用时   查找相应的文件,如果找不到则抛出错误   它

     

要测试问题是否是由WhiteNoise引起的,请尝试   交换Django的WhiteNoise存储后端:

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

同时尝试python manage.py collectstatic并检查问题是否仍然存在。

答案 1 :(得分:0)

tf.cond(full_init_cond, lambda: tf.no_op, lambda: tf.no_op) 所在的项目目录中,您是否会添加manage.py文件夹并在其中放置一个空白static文件?然后做一个git commit并推送。

enter image description here