51

I have a few pages. For every page I need load unique css. For all static files I use this. In the head of index.html I have:

{% block css  %}

{% endblock %}

But, for example, in contact.html I use:

{% extends "index.html" %}    
{% block css %}
    <link rel="stylesheet" href="{% static "css/contact.css" %}" type="text/css" />
    {% endblock %}

And its print error: Invalid block tag: 'static', expected 'endblock'. How to fix it?

3 Answers 3

99

You need to use {% load static %} first.

Sign up to request clarification or add additional context in comments.

5 Comments

Interesting that I still needed to load this even though I've 'extended' a template that has this 'function'. I feel like a child template should inherit this function if it doesn't override the block in which it is called. TL;DR - This actually works.
I find @JRM his question a very good question. Anyone knows why that is?
I've found this same thing! It doesn't seem very DRY to have to put in {% load whatever %} in every child template, possibly multiple times. Any word from team Django why this happens/if they're planning on fixing it?
@swizzard On the contrary. The DRY part is that it makes each template fragment independent, so it can be reused in multiple places, independent of whether or not the parent has loaded the library. As such, this is a feature, not a bug, and there is nothing to fix.
Thought there should be a link to the official docs regarding this - see Custom libraries and template inheritance.
0

it is

{% block css %}

{% endblock %}

1 Comment

Invalid block tag: 'static', expected 'endblock'
0

Your code needs to be refined.

You are using double quotes, where you need to use an apostrophe.

Use

href ="{% static 'css/contact.css' %} "

Instead of

href ="{% static" css/contact.css" %} "

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.