Django staticfiles not found on Heroku (with whitenoise)

This question seems to have been asked several times, but I cannot fix it.

I deployed a django application to create with DEBUG = False . I set allowed_host . I used {% load static from staticfiles %} to load static files. I definitely write the settings requested by the Heroku doc:

 BASE_DIR = os.path.dirname(os.path.dirname(__file__)) PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, 'static'), ) STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

BUT I got an error 500. And I got this trace (by mail)

 ... `cache_name = self.clean_name(self.hashed_name(name)) File "/app/.heroku/python/lib/python3.5/site- packages/django/contrib/staticfiles/storage.py", line 94, in hashed_name (clean_name, self)) ... ValueError: The file 'app/css/font.css' could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7febf600a7f0>.` 

When I ran heroku run python manage.py collectstatic --noinput Everything looks fine:

276 static files copied to '/app/annuaire/staticfiles', 276 post-processed.

Does anyone have an idea to help me please?

thanks

EDIT:

 annuaire |-- /annuaire |-- -- /settings.py |-- /app |-- -- /static/...` 

wsgi.py

 from django.core.wsgi import get_wsgi_application from whitenoise.django import DjangoWhiteNoise application = get_wsgi_application() application = DjangoWhiteNoise(application) 
+11
python django heroku django-staticfiles
source share
8 answers

I understood. I needed to add python manage.py collectstatic --noinput; in my procfile. Heroku dock said that collecticstatic will automatically fire. https://devcenter.heroku.com/articles/django-assets

thanks

+5
source share

With DEBUG=False , which originals used to work no longer work for me.

However, the fix, by allowing whitenoise to MIDDLEWARE in settings.py , solved it. It is best to be below SecurityMiddleware .

 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # add this line #Other middleware... ] 

`` ``

According to docs it needs to be included first.

+2
source share

for BASE_DIR you need a double name if your settings are not in the root directory and in / projectname / folder:

settings.py

 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' # for /static/root/favicon.ico WHITENOISE_ROOT = os.path.join(BASE_DIR, 'staticfiles', 'root') 

template.html

 {% load staticfiles %} <link rel="stylesheet" href="{% static "app/css/font.css" %}"> 

application tree for this example:

 annuaire |-- /annuaire |-- -- /settings.py |-- /app |-- /static/app/css/font.css 
+1
source share

I struggled for a couple of hours, until finally I understood the problem. The main problem, in my opinion, is that in the official Heroku documentation they use old-style MIDDLEWARE_CLASSES that uses MIDDLEWARE_CLASSES which is deprecated, instead of the new MIDDLEWARE setting.

In the white version 4+, the WSGI integration option for Django (which included editing wsgi.py) was removed. Instead, you should add WhiteNoise to your middleware list in settings.py and remove any WhiteNoise link from wsgi.py. (from documents )

The following configuration worked like a charm:

 STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' MIDDLEWARE = [ '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', 'django.middleware.security.SecurityMiddleware', # the next line of code is the one that solved my problems 'whitenoise.middleware.WhiteNoiseMiddleware', ] 

Note the following note, also from the docs.

You might find other third-party middleware that suggests that it should be given the highest priority at the top of the middleware list. If you do not understand exactly what is happening, you should ignore this advice and always place WhiteNoiseMiddleware on top of other middleware .

+1
source share

The problem is that the Heroku Python application uses the built-in web server and does not serve static files.

You can use the whitenoise application, it is a 100% working solution.

Suppose you have already created static files, for example:

 $ python manage.py collectstatic 

Then you need to do the following:

1) $ pip install whitenoise

2) add the line "whitenoise == 3.3.0" to requirements.txt

3) add code to settings.py

 STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 

4) add this code to app / wsgi.py

 from whitenoise.django import DjangoWhiteNoise application = DjangoWhiteNoise(application) 
0
source share

In addition to the answers above, it may also be that you did not specify the correct STATIC_ROOT , as described in https://docs.djangoproject.com/en/2.0/howto/static-files/#deployment

For me, the solution added this to the end of my setup settings.py

 STATIC_ROOT = "/app/static/" 

To find out where your static folder is in your hero, do

 heroku run python manage.py collectstatic 

Then you will see the path that will be shown there.

0
source share

For me after work.

settings.py

 DEBUG = True STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') #this is not used # Add static folder to STATIC_DIRS STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] 

urls.py

 from django.conf.urls.static import static from django.conf import settings urlpatterns = [ ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Note

This helper function works only in debug mode and only if the given prefix is ​​local (for example, / static /), and not a URL (for example, http://static.example.com/ ).

Also this helper function only serves the actual STATIC_ROOT folder; it does not search for static files like django.contrib.staticfiles.

0
source share

I had the same problem. The easiest way to find the problem is to use

 heroku run ls staticfiles/images 

if the images are in the directory where your files should be. This will give you a list of all the files in this directory.

As I found out, it was a problem with the file extension. The file had the extension .JPG and I referred to it in a template with the extension .jpg

0
source share

All Articles