Deepak
Deepak

Reputation: 23

Django static files not working on Heroku

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

Upvotes: 2

Views: 3135

Answers (3)

James
James

Reputation: 855

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/

Upvotes: 1

vijayscode
vijayscode

Reputation: 2015

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')

Upvotes: 1

Ajay Gupta
Ajay Gupta

Reputation: 1285


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'),
)

Upvotes: 1

Related Questions