简体   繁体   English

Django 放在根文件夹时找不到“/media/image.png”

[英]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.没有任何问题。

  1. Why can't it find ./media/image.png in the first part?为什么在第一部分找不到./media/image.png
  2. Is there a way to do the "static" trick but with another tag (say 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.

相关问题 "GET /media/img/image.png HTTP/1.1" 404 2575 - "GET /media/img/image.png HTTP/1.1" 404 2575 即使使用url(&#39;../../ images / image.png&#39;),背景图片路径也不起作用 - Background image path is not working even when I use url('../../images/image.png') CSS image.png未加载到Google驱动器上 - CSS image.png's not loading on google drive 如何根据用户性别显示两个不同的 image.png? - How to display two different image.png depending on users gender? 没有初始src的图片不会显示CSS背景图片:url(“ image.png”); - image without initial src doesn't display css background-image: url(“image.png”); iPhone上的HTML上传(iOS):文件名始终相同(image.jpg,image.png,...) - HTML Upload on iPhone (iOS): filename is always same (image.jpg, image.png, …) UIWebView背景图片:url(&#39;image.png&#39;)不起作用? - UIWebView Background-Image: url('image.png') doesn't work? body {background: url(image.png)} 不会居中或适应导航器大小? - body {background: url(image.png)} won't center or adapt to navigator size? “无法加载图像”来自 Django 的媒体文件夹 - “Could not load the image” from Django's media folder PNG图片在网站上不透明,无法找出原因 - PNG image not transparent on website, cannot find out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM