从静态文件django加载图片

时间:2020-06-07 22:08:13

标签: django

Django没有加载我的静态文件。我正在尝试从静态文件夹中加载图像,但是它不起作用。

file.html

<body>

      {% load static %}
      <img src="{% static "products/logo.jpg" %}" alt="My image">
</body>

结构

app
|_______products
       |_________templates
                 |_________file.html
       |_________models.py
       |_________ ...

|______static
       |_________products
                 |_________logo.jpg

设置

...
INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
    ...
]

...

STATIC_URL = '/static/'

2 个答案:

答案 0 :(得分:1)

您需要将其放入您的settings.py文件中:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

答案 1 :(得分:0)

<!DOCTYPE html>
<html>   
<head>
{% load static %}
</head>
<body>
<img src="{% static "products/logo.jpg" %}" alt="My image">
</body>
</html>

设置:

STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]