django-heroku服务器上未显示图像(媒体) [英] Images(Media) not displaying on django-heroku server

查看:88
本文介绍了django-heroku服务器上未显示图像(媒体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我成功上传图像文件并按设置正确获取图像路径但图像未显示时,我已经在Heroku服务器上上传了我的站点Django应用,这表明服务器中找不到错误的媒体文件 这是设置媒体设置

I had uploaded my site Django app on Heroku server when I upload image file is successfully uploaded and image path as per settings also fetch properly but the image is not displaying it give error media file not found in a server this is settings media setting

    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

这是在url.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('UserView.urls')),
    path('caterer/',include('CaterView.urls')),
]
# if settings.MediaAllow:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是models.py

this is models.py

class TypeofFood(models.Model):
    tyf_id = models.AutoField(primary_key=True,auto_created=True)
    tyf_value = models.CharField(max_length=256)
    tyf_image = models.ImageField(upload_to="typeoffood/", null=True, blank=True,default='default.jfif')

在模板中添加这样的图像

in template it fatch image like this

<center><img src="{{i.tyf_image.url}}" class="img-responsive" style="height: 200px; width: 200px; border-radius:50%" alt="Image of Caterers"></center>

推荐答案

heroku自由阶段不允许存储媒体文件,这就是为什么您的媒体文件在上传后将被删除

heroku free stage is not allow media file store, that's why your media file will be deleted after upload

因为这就像测试目的,如果您想在heroku上上传和存储媒体文件,则可以使用whitenoise之类的第三方,

because it is like testing purpose, if you want to upload and store media file on heroku you can use third party like whitenoise,

转到链接并了解如何使用whitenoise在heroku上上传媒体文件,您可以检查此链接也.

go to the link and learn how to use whitenoise to upload media file on heroku, you can check this link also.

快乐的打扮

这篇关于django-heroku服务器上未显示图像(媒体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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