2

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

)
psychok7
  • 5,373
  • 9
  • 63
  • 101
  • well can you give me more detail like application.urls also can you share the heroku logs when you hit the url – dusual Sep 02 '13 at 18:41
  • Also not sure if this is right for heroku 'postgres://localhost' – dusual Sep 02 '13 at 18:41
  • @dusual i am using django-oscar so i really don't have "quick" access to it. about the postgres, its the only way i got my django app to work on heroku. I think it has something to do with the images changing location or something – psychok7 Sep 02 '13 at 18:58

2 Answers2

6

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.

James Aylett
  • 3,332
  • 19
  • 20
  • thanks for the answer :) . is there a free storage service? amazon s3 is paid right? – psychok7 Sep 02 '13 at 19:50
  • S3 is paid (although it's almost never been a significant cost for me). No idea if there's a free alternative — you could check out the other backends provided in `django-storages` to see if any suit you better than S3. – James Aylett Sep 02 '13 at 19:51
  • i ran into some problems with your configuration, but this helped me solve it and also organize my static and uploaded files http://stackoverflow.com/questions/10390244/how-to-set-up-a-django-project-with-django-storages-and-amazon-s3-but-with-diff – psychok7 Sep 04 '13 at 21:16
  • for some reason the same behavior happens even tough i put everything on amazon. it works for a while but the second time i try to reload the page e gives me a 404. i did notice that the links are different (they now have ../cache/... ) in them.. any ideas?? – psychok7 Sep 04 '13 at 21:51
  • Are you using something like sorl for thumbnailing? That would generate cached versions. It's really hard to help without knowing a lot more about your configuration, I suspect. – James Aylett Sep 05 '13 at 23:37
  • i am using django-oscar for my ecommerce app which uses sorl if i am not mistaken. wich confs do you need me to post? i have more info here on another question http://stackoverflow.com/questions/18624609/django-heroku-media-files-404-error-with-amazon-s3 – psychok7 Sep 06 '13 at 07:53
0

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/

chipperdrew
  • 403
  • 4
  • 11
  • but i am having problems with the media files, not the static ones. This problem is only for the photos that i upload from my admin panel... i believe its not related, right? – psychok7 Sep 02 '13 at 19:45