问题
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