HTML: 图片未显示

huangapple go评论64阅读模式
英文:

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>

有什么想法,为什么图像根本不显示?

编辑

回应下面的问题,附加信息:

HTML: 图片未显示

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: &lt;img src=&quot;Bugs.jpg&quot;&gt;, 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.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;title&gt;B. Rabbit&lt;/title&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;

    &lt;h3&gt;Applications&lt;/h3&gt;

    &lt;ul&gt;
        &lt;li&gt;&lt;p&gt;&lt;a href=&quot;/polls&quot;&gt;A Polls Application.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
        &lt;li&gt;&lt;p&gt;&lt;a href=&quot;/hello&quot;&gt;Hello World Application.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;img src=&quot;Bugs.jpg&quot;&gt;

&lt;/body&gt;
&lt;/html&gt;

Any ideas why the image is not displaying at all?

Edit

Additional information in response to the below questions:

HTML: 图片未显示

HTML: 图片未显示

答案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 %}
&lt;html&gt;
    ...

And load image like this:

&lt;img src=&quot;{% static &quot;Bugs.jpg&quot;%}&quot;&gt;

Read docs to learn more:
https://docs.djangoproject.com/en/4.2/howto/static-files/

huangapple
  • 本文由 发表于 2023年5月28日 15:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定