django heroku media files 404 error

后端 未结 2 1165
情话喂你
情话喂你 2021-01-03 12:59

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

相关标签:
2条回答
  • 2021-01-03 13:47

    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/

    0 讨论(0)
  • 2021-01-03 13:49

    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.

    0 讨论(0)
提交回复
热议问题