jayt
jayt

Reputation: 768

In Django, how can I link a static file from within an extended HTML template?

I haven't found anything in the Django Docs or any Stack Overflow solutions to this same problem.

Say I have a html file like the following:

{% extends 'base.html' %}
{% block cssfile %}
<!-- css link goes here -->
{% endblock %}
{% block maincontent %}
Hello World
{% endblock %}

which will of course link to base.html. However, I want to put a css file between the 'block cssfile' tags, but given the way that Django is structured for linking static files, it prevents me from doing this.

Something like

{% block cssfile %}
<link rel="stylesheet"type="text/css"href="{% static 'myappname/css/style.css' %}"/>
{% endblock %}

will give me this error

TemplateSyntaxError at /myapp/1/ Invalid block tag on line 3: 'static', expected 'endblock'. Did you forget to register or load this tag?

I've also tried replacing the '{% %}' tags for the href with something like '{{ }}' for example.

Any help would be great, thank you.

Upvotes: 0

Views: 896

Answers (1)

carborgar
carborgar

Reputation: 171

You need to put {% load staticfiles %} before use {% static %}

Upvotes: 1

Related Questions