Django-生产中的媒体无法正常工作 [英] Django - Media in production is not working

查看:199
本文介绍了Django-生产中的媒体无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的静态文件运行良好,但我的媒体文件有问题.我已经在heroku中部署了我的网站,并且该网站已经投入生产. (DEBUG =假) 看一下代码:

My static files are working well, but my media ones have a problem. I already deployed my website in heroku and it is online in production. (DEBUG=False) Look at the code:

settings.py

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = bool(os.environ.get('DJANGO_DEBUG', True))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    # '/var/www/static/'
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
STATICFILES_STORAGE = 
'whitenoise.storage.CompressedManifestStaticFilesStorage'

urls.py

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, 
document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, 
document_root=settings.MEDIA_ROOT)

models.py

models.py

cartaz = models.FileField(default='/static/images/logo.jpg')

index.html

index.html

<img src="{{campeonato.cartaz.url}}" class="img-responsive">

我的Github存储库

这是问题所在,我在管理部分上传了图片,然后效果很好,但是几分钟后,图片突然从网站上消失了,出现了以下错误:

Here is the problem, I was uploading an image in the admin section and then it worked well, but suddenly after a couple minutes the image disappeared from the website and I got the following error:

Not Found:/media/tdscampeonatos_rzFCbET.jpg

然后,我在github中的项目中添加了一个媒体存储库,其中包含我上传的图像:

Then I added a media repository inside my project in github with the images I had uploaded:

添加媒体后,我的Github存储库

然后,我添加到媒体存储库中的图像可以正常工作,但是我没有添加的图像仍然无法正常工作.我上载了更多图像,但由于没有将图像直接转到在Github存储库中创建的媒体路径,因此出现了相同的错误. 5天试图解决这个问题,我已经在互联网上寻找几乎所有内容.请帮帮我!

Then the images I had added to the media repository worked, but the ones I didn't added was still not working. I uploaded some more images and I got the same error, because the images was not going directly to the media path I had created in the Github repository. 5 days trying to fix this, I already looked for almost everything in internet. Please help me!

推荐答案

您要在heroku上进行部署.可能是业余爱好计划.按照此计划,该应用程序将在不活动时进入睡眠状态,然后在访问时重新部署.因此,上次运行期间创建的媒体文件将被删除

You are deploying on heroku. Probably the hobby plan. On this plan, the app is made to sleep when not active then freshly redeployed when accessed. Thus the media files created during last run get deleted

您应该升级计划,或考虑使用cloudinary之类的第三方存储来存储媒体

You should upgrade your plan or consider using a thirdparty storage like cloudinary to store the media

这篇关于Django-生产中的媒体无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆