Adding an image into a django template from the static folder - Django

老子叫甜甜 提交于 2020-04-21 05:14:15

问题


I have a Django template and I want to add a static image to the file from my static folder that I have within the application. I am trying to but nothing is appearing on the template.

Does anyone know where my error is coming from?

Here is my code:

{% extends "base.html" %}

{% block content %}
<div class="row">
  <div class="col">
    <img src="static/images/Artboard1.png" alt="">
    <h2>{{ currentUser.username }}</h2>
  </div>

  <a href="{% url 'logout' %}">Logout</a>
{% endblock %}

Here is an image of my directory:


回答1:


load staticfiles tag to template after extends

{% extends "base.html" %}
{% load staticfiles %}

and then use it in src attribute of img HTML tag

<img src="{% static 'images/Artboard1.png'%}" alt="">



回答2:


Configure the following setting in your setting.py

STATIC_ROOT = os.path.join(BASE_DIR,"static_files")
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

And use in your template

<img src="{% static 'images/Artboard1.png'%}" alt="">

Hope this is help you



来源:https://stackoverflow.com/questions/46602422/adding-an-image-into-a-django-template-from-the-static-folder-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!