英文:
HTML: Image not displaying
问题
我正在使用Django。在以下项目文件夹中,我有一个.html
文件main.html
。
/home/bugs/django_projects/mysite/home/templates/home/main.html
在同一个文件夹中,我有一个名为Bugs.jpg的图像,例如:
/home/bugs/django_projects/mysite/home/templates/home/Bugs.jpg
我试图通过以下方式在网页中包含图像:<img src="Bugs.jpg">
,但图像不显示。我尝试通过以下方式进行调试:https://sebhastian.com/html-image-not-showing/,但似乎我引用了文件的正确位置。main.html
的完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title>B. Rabbit</title>
<meta charset="UTF-8">
</head>
<body>
<h3>Applications</h3>
<ul>
<li><p><a href="/polls">A Polls Application.</a></p></li>
<li><p><a href="/hello">Hello World Application.</a></p></li>
</ul>
<img src="Bugs.jpg">
</body>
</html>
有什么想法,为什么图像根本不显示?
编辑
回应下面的问题,附加信息:
英文:
I am using Django. I have an .html
file, main.html
, in the following project folder
/home/bugs/django_projects/mysite/home/templates/home/main.html
In the same folder I have an image Bugs.jpg. Eg
/home/bugs/django_projects/mysite/home/templates/home/Bugs.jpg
I am trying to include the image in the webpage via: <img src="Bugs.jpg">
, but the image does not display. I have tried debugging via: https://sebhastian.com/html-image-not-showing/, but I appear to be referencing the correct location of the file. The full code for main.html
is below.
<!DOCTYPE html>
<html lang="en">
<head>
<title>B. Rabbit</title>
<meta charset="UTF-8">
</head>
<body>
<h3>Applications</h3>
<ul>
<li><p><a href="/polls">A Polls Application.</a></p></li>
<li><p><a href="/hello">Hello World Application.</a></p></li>
</ul>
<img src="Bugs.jpg">
</body>
</html>
Any ideas why the image is not displaying at all?
Edit
Additional information in response to the below questions:
答案1
得分: 1
根据您的项目路径,我猜您正在使用Django项目。在Django中,加载静态文件有些不同。您应该在项目的根目录中创建一个名为static
的文件夹,并将图像放在其中。然后在settings.py中添加该文件夹,然后您可以像这样在HTML中加载图像文件:
在您的HTML文件的顶部添加以下内容:
{% load static %}
<html>
...
然后像这样加载图像:
<img src="{% static "Bugs.jpg" %}">
阅读文档以了解更多信息:
https://docs.djangoproject.com/en/4.2/howto/static-files/
英文:
Based on your project path, I guess you are working with a django project. In django , loading static files is a bit different. You should create a static
folder in root of your project and place the image there. Then add the folder in settings.py and then you can load image file in your html like this:
In top if you html file
{% load static %}
<html>
...
And load image like this:
<img src="{% static "Bugs.jpg"%}">
Read docs to learn more:
https://docs.djangoproject.com/en/4.2/howto/static-files/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论