我无法在 Django 模板中看到图像

问题描述 投票:0回答:2

我无法获取图片:

Screenshot

我已经签入了settings.py 文件和html 文件。一切都很好,但不起作用。我该如何解决这个问题?文件和CSS文件的路径一切正常。我还检查了代码,但一切都很好。检查告诉我该文件不存在,但它确实存在。

django django-rest-framework django-views django-templates django-settings
2个回答
0
投票

按照步骤操作

# -------- handle Static Files In Django --------

# Steps:
    # 1 - Create (static) folder in Root Directory of Project
    # 2 - Paste Your Static Files Folder in (static) Folder (js,css,images...etc)
    # 3 - Now Do Setting in (setting.py)
           STATIC_URL = '/static/'
           STATICFILES_DIRS = [BASE_DIR / 'static']
    # 4 - add {% load static %} tag on top of html page
    # 5 - Now use ({% static 'assets/image.jpg' %}) tag in HTML File for calling static files
    # 6 - Done

0
投票

写在模板的顶部

{% load static %}

然后使用

{% static 'img/myimage.png' %}
来寻址您的文件。

© www.soinside.com 2019 - 2024. All rights reserved.