Skip to main content
 首页 » 编程设计

python之部署后未加载 Wagtail 图像

2024年10月01日9yxwkf

我不太确定发生了什么,但我的图像在我的本地开发服务器上加载得很好。但是,一旦我部署,图像将不会加载。我检查了 URL,它似乎是正确的。我不确定这里发生了什么。

base.py

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

HTML

{% load wagtailcore_tags wagtailimages_tags %} 
... 
{% with post.main_image as main_image %} 
    {% if main_image %}{% image main_image fill-400x200 %}{% endif %} 
{% endwith %} 

元素的页面源代码

<img alt="photo" src="/media/images/Lily.2e16d0ba.fill-400x200.jpg" width="400" height="201"> 

我很困惑,我真的找不到太多关于这个话题的信息。我是唯一遇到这个问题的人吗?任何帮助将不胜感激。

请您参考如下方法:

在开发期间(当您使用 ./manage.py runserver 并将 DEBUG 设置设置为 True 时),Django serves static files itself作为方便。在生产环境中,由您来配置 Web 服务器以提供来自 /media 和 /static 的静态文件:https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/

这是出于性能和安全原因——当已经有一个为该任务适当调整的网络服务器时,通过 Python 代码提供静态文件是没有意义的。