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

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

问题描述

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



我可以看到保存在静态字段中的图像我的图片使用以下命令:

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

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

 < img src ={{instance.image.url}}class =img响应性> 

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

  instance = get_object_or_404(Post,slug = slug)
if instance.draft or instance.publish> timezone.now()。date():
如果不是request.user.is_staff或不是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上运行应用程序,如果没有请求,30分钟后dynos会进入休眠状态,这使得Heroku不会在dynos重启之间保留用户上传媒体文件。

解决方案



使用Amazon S3服务进行存储并提供媒体文件+您还可以从S3提供静态文件。请参阅


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">

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)

Thanks

解决方案

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.

Solution

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天全站免登陆