2017-01-04 58 views
1

在heroku域上,我无法加载我的媒体(通过使用ImageField属性保存的图像)文件图像。但是,我能看到保存在静态字段的图像,如果我设置debug = True将Heroku用于Django媒体文件

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

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

,我能够做参考他们在我的模板someething这样的:

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

凡实例从我views.py通过像这样:

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) 

感谢

+0

您仍然没有提供任何关于如何尝试提供这些文件的信息。 –

回答

2

原因

Heroku的运行您的DYNOS和DYNOS应用走30分钟后睡觉,如果没有请求到来时,这使得Heroku的不保存重启DYNOS之间的用户上传媒体文件。

解决方案

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