将 Heroku 用于 Django 媒体文件 [英] Using Heroku for Django Media Files

查看:27
本文介绍了将 Heroku 用于 Django 媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 heroku 域上,我无法加载我的媒体(使用 ImageField 属性保存的图像)文件图像.但是,如果我设置 debug = True

On the heroku domain, I am not able to load my Media(Images Saved by using ImageField property) file images. However, I am able to see the images saved in the static field if I set debug = True

我使用以下命令保存我的图像:

I save my images using the following command:

image = models.ImageField(upload_to=upload_location, null=True, blank=True, width_field="width_field", height_field = "height_field")

我可以通过做这样的事情在我的模板中引用它们:

And I am able to reference them in my template by doing someething like this:

<img src="{{ instance.image.url }}" class="img-responsive">

实例是从我的 views.py 传递的,如下所示:

Where instance is passed from my views.py like so:

instance = get_object_or_404(Post,slug=slug)
if instance.draft or instance.publish > timezone.now().date():
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
share_string = quote_plus(instance.content)
context = {
    "title": "Detail",
    "instance": instance,
    "share_string":share_string,
}
return render(request,"post_detail.html",context)

谢谢

推荐答案

原因

Heroku 在 dynos 上运行您的应用程序,如果没有请求,则 dynos 会在 30 分钟后进入睡眠状态,这使得 Heroku 在 dynos 重启之间无法保留用户上传的媒体文件.

Reason

Heroku runs your application on dynos and dynos go to sleep after 30 minutes if there is not request comes, this makes Heroku not preserve user upload media files between dynos restart.

使用 Amazon S3 服务来存储和提供媒体文件 + 您也可以从 S3 提供静态文件.有关文档,请参阅此处.

Use Amazon S3 service for storing and serving media files + you can also serve static files from S3 also. for documentation refer here.

这篇关于将 Heroku 用于 Django 媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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