[英]Django cannot find “/media/image.png” when placed in root folder
I have the following structure我有以下结构
my app/
|_my_app/
| |_templates/
|_static/
|_media/
|_utils/
|_main.py
|_manage.py
|_.gitignore
and having the in template.html
并具有template.html
<img src="./media/image.png">
then it casts a GET 404 error Cannot find /media/image.png
. <img src="./media/image.png">
然后它会抛出 GET 404 错误Cannot find /media/image.png
。
I have also tried relative path (relative to the template ie <img src="../../media/image.png">
.我也尝试过相对路径(相对于模板,即<img src="../../media/image.png">
。
Moving the media folder into static (and including {% load static %}
I can do将媒体文件夹移动到 static (包括{% load static %}
我可以
<img src="{% static '/media/image.png'">
without any issues.没有任何问题。
./media/image.png
in the first part?为什么在第一部分找不到./media/image.png
?media
) eg <img src="{% media 'image.png' ">
to avoid absolute paths?有没有办法做“静态”技巧,但使用另一个标签(比如media
)例如<img src="{% media 'image.png' ">
以避免绝对路径?You can create a new folder named img in the static folder, then paste your image in the img folder such that the path to the image will be like this: static -> img -> image.png .您可以在 static 文件夹中创建一个名为img的新文件夹,然后将图像粘贴到img文件夹中,使图像的路径如下所示: static -> img -> image.png 。 You can load the image in HTML like this:您可以像这样在 HTML 中加载图像:
<img src="{% static 'img/image.png'">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.