3

I get Invalid block tag: 'static', expected 'endif' when I render the following template

{% load staticfiles %} <!-- in base.html -->/
 <!-- ... -->
{% block content %}
    {% if ava_url %}
        <div><img src="{{ava_url}}"></div>
    {% else %}
        <div><img src="{% static 'img/default_ava.png' %}"></div>
    {% endif %}
{% endblock %}

Why cannot I nest static under if?

Using Django 1.6.7

upd This is all in block container. Might be the reason. However when I remove if

{% load staticfiles %}  <!-- in base.html -->
 <!-- ... -->
{% block content %}

    <div><img src="{% static 'img/default_ava.png' %}"></div>

{% endblock %}

I get Invalid block tag: 'static', expected 'endblock'

upd solved. included {% load staticfiles%} in derived template. Sorry, it was not obvious, that {% load staticfiles%} was in base template in the first place. The answer was found here

However I need further explanation: why {% load staticfiles%} does not work from base template?

Community
  • 1
  • 1
Sashko Lykhenko
  • 1,554
  • 4
  • 20
  • 36

1 Answers1

6

Think of it like an import statement where the base template is a function within one module that is calling a function in another module, the derived template, with some parameters.

The "import" in the base template doesn't get inherited by the derived template and thus it needs to be "imported" with {% load ... %}

Adam Kerz
  • 919
  • 1
  • 6
  • 14