找不到HTML图片(django) [英] HTML image not found (django)
问题描述
我一直在面对这个问题,我遇到错误,告诉我找不到图像,并且在网站上出现了破裂的图片.我将附加代码,包括目录以及出现错误的终端.
I've been facing this problem, where i get error telling me image not found, and i get cracked picture on the website. I will attach the code including the directory and also the terminal where I get the error.
推荐答案
这是因为在Django中,您使用的是服务器,因此它需要知道在哪里查找文件以及在哪里提供文件.
That's because with Django you use a server, and it needs to know where to look for the files and where to serve them.
默认情况下,它将查找名为"static"的文件夹,因此您应创建一个名为"static"的目录,并将图像放在其中.
It will by default look into a folder called "static", so you should make a directory called "static" and put your images there
然后在settings.py中定义Django将在其中提供这些文件的路径:
Then define a path where Django will serve these files in settings.py:
STATIC_URL = '/static/'
每当需要将图像添加到模板时,都应添加以下代码:
Whenever you need to add an image to your templates, you should add the following code:
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
{%load static%}仅需要在模板文件的顶部被调用一次.
the {% load static %} only needs to be invoked once, at the top of your template file.
https://django.readthedocs.io/en/2.2.x/howto/static-files/
这篇关于找不到HTML图片(django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!