简体   繁体   English

在heroku中使用dj-static提供媒体文件

[英]serving media files with dj-static in heroku

I'm trying to serve media files that are registered in django-admin. 我正在尝试提供在django-admin中注册的媒体文件。

When accessing an image by api error 404 Not found. 通过api错误访问图像404时找不到。

I made a configuration as the recommended documentation , but in heroku does not work. 我进行了配置,作为推荐的文档 ,但是在heroku中不起作用。

settings.py 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 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 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 ). 我遇到了同样的问题,并通过将我在models.py上的路径更改为其他路径来解决了该问题。它已配置为通过media / images / img.jpg访问 ,但是使用dj-static的页面正在从与静态文件相同的文件夹结构,应位于myapp / media / images / img.jpg (静态文件是同一件事,但使用static而不是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). (请记住将myapp更改为您的应用名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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