简体   繁体   English

在生产 Django-Heroku 中找不到媒体文件:返回 404 错误

[英]Media file not being found in production Django-Heroku: returns 404 error

I have some default images in my media folder in Djang.我在 Djang 的媒体文件夹中有一些默认图像。 However, on my website, I cannot see the images being displayed and I get a 404 error.但是,在我的网站上,我看不到正在显示的图像,并且出现 404 错误。

Now my project folder tree is(project name is register):现在我的项目文件夹树是(项目名称是注册):

register
-- live-static
---- static-root
---- media-root
------ defaults

and my media and static files settings are:我的媒体和静态文件设置是:

STATIC_URL = '/static/'
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'live-static', 'static-root')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'live-static', 'media-root')

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

And inside my urls.py in the same folder as wsgi.py I have:在我的urls.pywsgi.py相同的文件夹中,我有:

urlpatterns = [ 
    """
    My other urls
    """ 
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

My static files are loaded and the server can retrieve them, however, the images that I added to my folders in media-root are returning a 404 error by the server.我的静态文件已加载,服务器可以检索它们,但是,我添加到media-root文件夹中的图像由服务器返回 404 错误。 Why is this happening as I have already also set STATICFILES_STORAGE as well?为什么会发生这种情况,因为我也已经设置了STATICFILES_STORAGE

This is something I got hung up on with my first Heroku deploy.这是我第一次部署 Heroku 时遇到的问题。 Since Heroku projects run on dynos, they eventually go to sleep if there is not a request within a certain period of time.由于 Heroku 项目在 dynos 上运行,如果在一段时间内没有请求,它们最终会进入睡眠状态。 When the dyno restarts, it does not save the uploaded data.当测功机重启时,它不会保存上传的数据。 Here is a link to some information from Heroku: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted以下是 Heroku 部分信息的链接: https : //help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted

What I ended up doing is setting up an Amazon S3 bucket for storage of these files.我最终做的是设置一个 Amazon S3 存储桶来存储这些文件。 Dennis Ivy did a video explaining how to do this: https://youtu.be/inQyZ7zFMHM Dennis Ivy 做了一个视频来解释如何做到这一点: https : //youtu.be/inQyZ7zFMHM

Update your setting file like this像这样更新您的设置文件

there is no need to change any thing else无需更改任何其他内容

in urls.py change the urlpatterns to.在 urls.py 中将 urlpatterns 更改为。 remove STATIC_ROOT etc删除 STATIC_ROOT 等

+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

in setting.py like this.在 setting.py 这样。 remove other file settings like STATICFILES_STORAGE删除其他文件设置,如 STATICFILES_STORAGE

MEDIA_URL = '/media/'
STATIC_URL = '/static/'



STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]
#if your media folder is in public_html which contain the media file
MEDIA_ROOT = '/home/yourusername/public_html/media'
STATIC_ROOT = '/home/yourusername/public_html/static'

#if your media folder is in other_folder which contain the media file
MEDIA_ROOT = '/home/yourusername/other_folder /media'
STATIC_ROOT = '/home/yourusername/other_folder /static'

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

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