2017-01-04 57 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.