2

My django application is working well on local server. But, when I deploy it on Heroku, the static files are not being served (getting a 404 error). Please help!

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = patterns('',
       url(r'^$', 'product.views.home', name='home'),
       url(r'^admin/', include(admin.site.urls)),

   )
if settings.DEBUG:
       urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
       urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

static files settings:

  TEMPLATE_DIRS = (
      os.path.join(BASE_DIR, 'templates'),
  )

  STATIC_URL = '/static/'

  MEDIA_URL = '/media/'

  MEDIA_ROOT = os.path.join(BASE_DIR, "static", "media")

  STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root")

  STATICFILES_DIRS = (
      os.path.join(BASE_DIR, "static", "static_dirs"),
  )

WSGI file -

  import os
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "acton.settings")

  from django.core.wsgi import get_wsgi_application
  application = get_wsgi_application()

  try:
      from dj_static import Cling
      application = Cling(get_wsgi_application())

  except:
      pass
Jeril
  • 7,858
  • 3
  • 52
  • 69
Deepak
  • 23
  • 1
  • 5

3 Answers3

1


This is my setting for static files to deploy on Heroku.

Hope it will help :

import os
BASE_DIR = os.path.dirname(os.path.abspath(file))
STATIC_ROOT = 'staticfiles'
STATIC_URL ='/static/'
MEDIA_ROOT = os.path.join(PROJECT_PATH, "media")
MEDIA_URL = "/media/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

Ajay Gupta
  • 1,285
  • 8
  • 22
  • Still having the same issue! – Deepak Jul 05 '15 at 19:41
  • can you tell if you are getting any error while deployment, on your terminal regarding running of python manage.py collectstatic command? – Ajay Gupta Jul 05 '15 at 19:44
  • No, when I run 'heroku run python manage.py collectstatic' - I see no error, static files are collected. What weird is css files for admin on heroku is working. Its only the front end I am having issues. – Deepak Jul 05 '15 at 20:37
  • change your STATIC_ROOT='staticfiles' to settings which i have written. There is no other way i see an issue in there. Since collectstatic collects all files in your 'staticfiles' folder in your project root. STATIC_URL is the path by which yo access them. – Ajay Gupta Jul 06 '15 at 05:51
  • I deleted the Heroku app completely and re-deployed from scratch. Its working now! Thank you for the suggestion. – Deepak Jul 07 '15 at 06:45
1

Your setting.py file is incorrectly configured.Static and media files should be

STATICFILES_DIRS = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
vijayscode
  • 1,905
  • 4
  • 21
  • 37
  • With my settings.py , the static files are working well on the development server. Its only on the Heroku production server I am having issue. Now, when I change the settings.py as per your recommendation, my static files are not working on development server! – Deepak Jul 05 '15 at 19:41
  • Check http://stackoverflow.com/questions/21141315/django-static-files-on-heroku and http://stackoverflow.com/questions/21141315/django-static-files-on-heroku – vijayscode Jul 05 '15 at 19:50
  • I checked all these questions and tried their recommendation with no success. The static files are successfully collected on Heroku, but its not displaying. – Deepak Jul 05 '15 at 20:40
  • What was the path that you were giving to your static files in your HTML templates? – Ankush Raghuvanshi Jul 29 '16 at 03:26
1

For anyone else that comes across this problem, for me it was that I was missing the whitenoise configuration from my wsgi.py file.

Specifically the following was missing from my wsgi.py file:

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

Docs are here: http://whitenoise.evans.io/en/stable/

James
  • 815
  • 1
  • 13
  • 24