简体   繁体   中英

serving media files with dj-static in heroku

I'm trying to serve media files that are registered in django-admin.

When accessing an image by api error 404 Not found.

I made a configuration as the recommended documentation , but in heroku does not work.

settings.py

import os
from decouple import config, Csv
from dj_database_url import parse as dburl

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())

default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
DATABASES = {
    'default': config('DATABASE_URL', default=default_dburl, cast=dburl),
}

(...)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

wsgi.py

import os
from dj_static import Cling, MediaCling
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "integrafundaj.settings")

application = Cling(MediaCling(get_wsgi_application()))

requirements.txt

dj-database-url==0.4.2
dj-static==0.0.6
Django==1.11.3
djangorestframework==3.7.7
easy-thumbnails==2.4.2
olefile==0.44
Pillow==4.3.0
python-decouple==3.1
pytz==2017.3
static3==0.7.0
Unidecode==0.4.21
gunicorn==19.4.1
psycopg2==2.7.1

I had the same issue and fixed it by changing my path on models.py to a different one... It was configured to access it through media/images/img.jpg , but the page using dj-static was requesting it from the same folder structure as static files, which should be located at myapp/media/images/img.jpg (static files were the same thing but with static instead of media ).

After this change, and after re-uploading the images to the new folder, everything worked fine! (remember to change myapp to the your app's name).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM