0

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:

enter image description here

2
  • in which python file are you trying to add it? Commented Oct 6, 2017 at 9:20
  • 1
    For a start, you need an absolute path: /static/images/Artboard1.png. Also how have you configured static files? Is this is dev or production? Commented Oct 6, 2017 at 9:41

3 Answers 3

8

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="">
Sign up to request clarification or add additional context in comments.

1 Comment

@Sandeep it will work with {%load static %} as well.
7

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

1 Comment

I implimented the code that you have and got this message Invalid block tag on line 6: 'static', expected 'endblock'. Did you forget to register or load this tag? @nikhilrane
0

I had the same problem. I sorted it out by adding {% load static %} to every subpage.

like here:

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

{% block title %}
{{ title }}
{% endblock title %}

{% block hero %}
<picture class="hero__background">
    <img src="{% static 'top_banner.jpg' %}" alt="" srcset="">    
</picture>
{% endblock hero %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.