Search code examples
djangotagsblockstatic-content

loading an image using the static tag from a static path


I am unable to load an image on my index page using the following syntax

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}

    <-!---This is the statement-->
   <-div data-src=  "{% static "bootstrap/images/slider/slider-img1.jpg" %}">
   <-/div>

{% endblock content %}

I added an extra - (dash) in the div because the post had difficulty displaying it.

Anyways I am getting the error

Invalid block tag on line 69: 'static', expected 'endblock'. Did you forget to register or load this tag?

I think this is a directory structure. My directory currently looks like this

|_mainApp
|  |_______index.html ------>This is where i am trying to run the statement
|_templates
|  |_______base.html ------->The statement works fine in the base
|_static
   |_bootstrap
|_CenterApp
   |_settings.py
   |_urls.py

Any suggestions on why I am getting that error is that a location issue and the content cant be found ? or is it something else ?


Solution

  • Whenever you use {% extend "some_page.html" %} and you need some links with static url you can use {{STATIC_URL}} instead of {% static "/some/link/" %}

    So in your case, use it as:

    <-div data-src=  "{{STATIC_URL}}bootstrap/images/slider/slider-img1.jpg">