Sanjeev Siva
Sanjeev Siva

Reputation: 984

Heroku server error (500) when Debug = False , whitenoise could not find style.css

I am getting a Server Error(500) when debug is set to false, But the site works fine when debug=True

This is the Heroku log: [EDIT:NEW LOG FILE AFTER MAKING CHANGES IN DJANGO LOG]

2017-07-30T17:19:06.000000+00:00 app[api]: Build succeeded
2017-07-30T17:19:25.874517+00:00 heroku[web.1]: State changed from up to starting
2017-07-30T17:19:25.873834+00:00 heroku[web.1]: Restarting
2017-07-30T17:19:26.809034+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2017-07-30T17:19:26.828322+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [11] [INFO] Worker exiting (pid: 11)
2017-07-30T17:19:26.842100+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [10] [INFO] Worker exiting (pid: 10)
2017-07-30T17:19:26.852227+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [4] [INFO] Handling signal: term
2017-07-30T17:19:26.868822+00:00 app[web.1]: [2017-07-30 17:19:26 +0000] [4] [INFO] Shutting down: Master
2017-07-30T17:19:27.014793+00:00 heroku[web.1]: Process exited with status 0
2017-07-30T17:19:30.765424+00:00 heroku[web.1]: Starting process with command `gunicorn techzu.wsgi --log-file -`
2017-07-30T17:19:33.522804+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [4] [INFO] Starting gunicorn 19.7.1
2017-07-30T17:19:33.523786+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [4] [INFO] Using worker: sync
2017-07-30T17:19:33.523653+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [4] [INFO] Listening at: http://0.0.0.0:43129 (4)
2017-07-30T17:19:33.528197+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [10] [INFO] Booting worker with pid: 10
2017-07-30T17:19:33.549206+00:00 app[web.1]: [2017-07-30 17:19:33 +0000] [11] [INFO] Booting worker with pid: 11
2017-07-30T17:19:34.484665+00:00 heroku[web.1]: State changed from starting to up
2017-07-30T17:20:18.368438+00:00 heroku[router]: at=info method=GET path="/" host=www.tecbux.com request_id=84831c2e-e908-4313-8ea0-3a689a54e3a4 fwd="49.207.184.93" dyno=web.1 connect=0ms service=134ms status=500 bytes=239 protocol=http
2017-07-30T17:20:18.362963+00:00 app[web.1]: Traceback (most recent call last):
2017-07-30T17:20:18.362946+00:00 app[web.1]: Internal Server Error: /
2017-07-30T17:20:18.362964+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
2017-07-30T17:20:18.362965+00:00 app[web.1]:     response = get_response(request)
2017-07-30T17:20:18.362965+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
2017-07-30T17:20:18.362967+00:00 app[web.1]:     response = self.process_exception_by_middleware(e, request)
2017-07-30T17:20:18.362967+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
2017-07-30T17:20:18.362968+00:00 app[web.1]:     response = response.render()
2017-07-30T17:20:18.362969+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 109, in render
2017-07-30T17:20:18.362969+00:00 app[web.1]:     self.content = self.rendered_content
2017-07-30T17:20:18.362972+00:00 app[web.1]:     return self.template.render(context)
2017-07-30T17:20:18.362970+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 86, in rendered_content
2017-07-30T17:20:18.362971+00:00 app[web.1]:     content = template.render(context, self._request)
2017-07-30T17:20:18.362971+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
2017-07-30T17:20:18.362975+00:00 app[web.1]:     return self.nodelist.render(context)
2017-07-30T17:20:18.362973+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 208, in render
2017-07-30T17:20:18.362973+00:00 app[web.1]:     return self._render(context)
2017-07-30T17:20:18.362974+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
2017-07-30T17:20:18.362976+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
2017-07-30T17:20:18.362978+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/static.py", line 104, in render
2017-07-30T17:20:18.362975+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 994, in render
2017-07-30T17:20:18.362978+00:00 app[web.1]:     url = self.url(context)
2017-07-30T17:20:18.362976+00:00 app[web.1]:     bit = node.render_annotated(context)
2017-07-30T17:20:18.362980+00:00 app[web.1]:     return self.handle_simple(path)
2017-07-30T17:20:18.362977+00:00 app[web.1]:     return self.render(context)
2017-07-30T17:20:18.362981+00:00 app[web.1]:     return staticfiles_storage.url(path)
2017-07-30T17:20:18.362979+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/static.py", line 101, in url
2017-07-30T17:20:18.362981+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/static.py", line 114, in handle_simple
2017-07-30T17:20:18.362982+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 132, in url
2017-07-30T17:20:18.362983+00:00 app[web.1]:     hashed_name = self.stored_name(clean_name)
2017-07-30T17:20:18.362983+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 292, in stored_name
2017-07-30T17:20:18.362984+00:00 app[web.1]:     cache_name = self.clean_name(self.hashed_name(name))
2017-07-30T17:20:18.362985+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 95, in hashed_name
2017-07-30T17:20:18.362985+00:00 app[web.1]:     (clean_name, self))
2017-07-30T17:20:18.362988+00:00 app[web.1]: ValueError: The file 'style.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f6decd24b50>.

At first i had an Internal server error, but that was because i did not put my SENDGRID API in my settings.py, After adding that i get SERVER ERROR(500)

This is the settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = '****'
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = "sgbackend.SendGridBackend"
SENDGRID_API_KEY = "***"

ALLOWED_HOSTS = ['www.xxx.com', 'xxx.com']

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
 ###
 ]

     DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

import dj_database_url

db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

MEDIA_URL = '/media/'
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_JQUERY_URL = '//code.jquery.com/jquery-3.1.1.min.js'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'static/media')

The Static files are loaded in Debug=False

This is when i try heroku local web

 [WARN] No ENV file found
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7476] [INFO] Starting gunicorn 19.7.1
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7476] [INFO] Listening at: http://0.0.0.0:5000 (7476)
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7476] [INFO] Using worker: sync
    22:19:44 web.1   |  [2017-07-29 22:19:44 +0000] [7482] [INFO] Booting worker with pid: 7482

I get a BAD REQUEST (400)

PS : So,some of the images are not loading too, could this be an issue with static files? But my CSS loads along with some other images.

[EDIT]The Images That are not loading are the images i upload through django's admin page when it is live. After git push heroku master the images aren't loading.

This is my wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "techzu.settings")

application = get_wsgi_application()

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

[EDIT : ValueError: The file 'style.css' could not be found with .]

Upvotes: 3

Views: 15524

Answers (6)

Guddi
Guddi

Reputation: 31

In my case it was due to conflict between django staticfiles and django _heroku staticfiles

I had to disable one of them.

Either do this,

 ` INSTALLED_APPS={
            .
            .
            #django.contrib.staticfiles,
  }
  django_heroku.settings(locals())` #in end of settings.py

Or do this

    `INSTALLED_APPS={
            .
            .
            django.contrib.staticfiles,
      }
     django_heroku.settings(locals(),staticfiles=False)` #in end of settings.py

Or if you are using whitenoise ,disable staticfiles for both django and django_heroku.

Upvotes: 3

BugHunter
BugHunter

Reputation: 1204

While turning off Debugging. You need to provide ALLOWED_HOSTS in a list. Please check Django documentation for more..

Debug = False
ALLOWED_HOSTS = ['xyz.com']

Upvotes: 4

Marvelous Ikechi
Marvelous Ikechi

Reputation: 2043

Try making migrations with:

heroku run python manage.py makemigrations

heroku run python manage.py migrate

I had the same issue, but this solved it.

Upvotes: 8

Sanjeev Siva
Sanjeev Siva

Reputation: 984

Added this in the settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
             'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
        },
    },
}

Got the errors in the log Apparently there was a mismatch with the names of the folders in the remote server and my local machine

Upvotes: 14

Elinaldo Monteiro
Elinaldo Monteiro

Reputation: 735

You need to provide ALLOWED_HOSTS

Debug = False
ALLOWED_HOSTS = [".herokuapp.com"]

And Heroku you should use PostgreSQL

https://github.com/kennethreitz/dj-database-url

Upvotes: 3

pydanny
pydanny

Reputation: 8324

You can't use SQLite3 on Heroku. Switch to Postgres.

Upvotes: 3

Related Questions