I recently deployed a Django app to Heroku and uploaded some media files and everything seemed to work fine, until yesterday when i tried to access the application again and saw that it was giving a 404 error.
Any ideas why this is happening?
settings.py:
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
import dj_database_url
#DATABASES['default'] = dj_database_url.config()
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
urls.py
urlpatterns = patterns('',
(r'', include(application.urls)),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
)
Heroku dynos are of limited lifespan, and when they die and get replaced (which happens automatically) any files within them are lost, including any files you uploaded via Django. What you want to do is to set up Django's media handling to put the files somewhere more permanent (which will also allow you to use multiple dynos at once, which is how Heroku tackles horizontal scaling). I tend to use Amazon S3 for this, so my configuration looks a little like:
AWS_STORAGE_BUCKET_NAME = "your_bucket"
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
MEDIA_URL = "https://%s.s3.amazonaws.com/" % os.environ['AWS_STORAGE_BUCKET_NAME']
MEDIA_ROOT = ''
AWS_ACCESS_KEY_ID = "your_access_key_id"
AWS_SECRET_ACCESS_KEY = "your_secret_access_key"
This is using django-storages
and boto to provide a Django storage layer using Amazon S3.
Note that this "pass-through" access for S3 may be inappropriate depending on your application. There are some notes on working with S3 in Heroku's devcenter that may help.
My guess would be that something is off with your static files.
For example, you have
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
For my Heroku app, I have
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Settings for static files is something that few people seem to really understand (including myself), but this blog post offers a pretty good explanation: http://blog.doismellburning.co.uk/2012/06/25/django-and-static-files/