0

I am trying to put an image in html template in my django applicarion but its is not displayed. When i inspect, it says image cannot be loaded.

HTML code:

<div class = "col-md-4">
  <img src= "/static/images/abc.jpg" alt="sorry"/>
  </div>

location of the image abc.png is /home/user/demo/mysite/mysite/static/images/abc.png and my django application is bookmark which resides in mysite(demo/mysite)

Is it the correct way of giving path in a django application or something else needs to be done.

.
└── mysite
    ├── bookmark
    │   ├── admin.py
    │   ├── admin.pyc
    │   ├── apps.py
    │   ├── forms.py
    │   ├── forms.pyc
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── 0001_initial.pyc
    │   │   ├── 0002_auto_20171029_1436.py
    │   │   ├── 0002_auto_20171029_1436.pyc
    │   │   ├── 0003_userbookmark.py
    │   │   ├── 0003_userbookmark.pyc
    │   │   ├── __init__.py
    │   │   └── __init__.pyc
    │   ├── models.py
    │   ├── models.pyc
    │   ├── static
    │   │   └── style.css
    │   ├── templates
    │   │   ├── 20150225_134637.jpg
    │   │   ├── abc.jpg
    │   │   ├── base_generic.html
    │   │   ├── category.html
    │   │   ├── index.html
    │   │   ├── login.html
    │   │   ├── myprofile.html
    │   │   ├── profile.html
    │   │   ├── register2.html
    │   │   ├── register.html
    │   │   ├── registerInterest.html
    │   │   ├── sample
    │   │   └── success.html
    │   ├── tests.py
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── views.py
    │   └── views.pyc
    ├── db.sqlite3
    ├── manage.py
    ├── mysite
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── settings.py
    │   ├── settings.pyc
    │   ├── static
    │   │   └── images
    │   │       ├── abc.jpg
    │   │       └── [email protected]
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── wsgi.py
    │   └── wsgi.pyc
    └── urls.py

I have edited the post and added the tree structure of my project

4

1 Answer 1

0

This is correct way to use static files in django templates

{% load static %}

<div class="col-md-4">
    <img src= "{% static "images/abc.jpg" %} alt="sorry"/>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this as well but did not work. do we need to change settings.py file as well?

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.