The code below makes error.. How could I resolve this problem?
{% block header %}
<link rel="stylesheet" href="{% static 'shop/style.css' %}" />
{% endblock %}
The error output:
TemplateSyntaxError : Invalid block tag: 'static', expected 'endblock'
Just add
{% load static %}
to the top of your template after the{% extends 'app/base.html' %}
.Yes. Django won't allow it.
You can just use the appropriate path like:
But be aware: If you change your app's
STATIC_URL
, thehref
above must also be updated accordingly.From Configuring static files:
If you are you are using Apache, make sure you have configured the virtual host to serve static files, for example in
000-default.conf
1.) in settings.py add A TUPLE :
STATIFILES_DIR = ( os.path.join(BASE_DIR,'assets') , )
2.) in urls.py add :
3.) in the html file where you are putting the "link rel='stylesheet' .." , just add at the top :
My solution is to
include
another page with{% load static %}
and script with static reference.{% block xxx %}
expects the first{% yyy %}
not to be other than{% include %}
and{% endblock %}
(the only cases I have observed); so when we use"{% static 'xxx.js' %}"
it breaks and complains. But including another page will put Django in calm.For example, I have a page
homepage
which extendsbase.html
and has some static js files which are not included inbase.html
.base.html
homepage.html
:home_js.html
:Now the scripts loads.
So, in a block we cannot use
{% %}
tags other than{% block xxx %}
and{% endblock %}
.I am using Django 5.1.
EDIT:
I found
{% verbatim %}
tag to be our savior under such situation.No, it is not impossible. Try including
{% load staticfiles%}
in the same html file, rather than attempting to inherit it from somebase.html
.