Link to CSS and JS files other than in base.html

179 views
Skip to first unread message

Bobo

unread,
Sep 8, 2008, 6:47:24 AM9/8/08
to Django users
Hi everyone,

I’ve a question about loading CSS and JS files. The normal command for
doing so is:

<link rel="stylesheet" type="text/css" href="/site_media/css/
MY_CSS_FILE.css" />
<script type="text/javascript" src="/site_media/js/MY_JS_FILE.js"></
script>

I’m doing that in my Base.html because it’s the only HTML file which
contain a <head></head> tag.

All the other templates just extend that.

My question now, is there any way that I can make some kind of
template specified CSS/JS links?

For example; I’ve a template called addactivity.html where it is
possible from a form to submit a new activity. I’ve used some AJAX, a
special CSS form file and a JS file. It is the only template I’m using
the JS file and the CSS file. In my Base.html I’m currently importing
about 7-8 CSS files and about 4-5 JS files. That is of cause silly
since some of them only are used once or twice.

Can I load the files in the specific template or can I somehow dictate
base.html to only load some files when one template is active and
another file with the next template and so on.

Thanks
Bobo

Scott Moonen

unread,
Sep 8, 2008, 6:50:08 AM9/8/08
to django...@googlegroups.com
Hi,

What I do is put an empty {% block extrahead %}{% endblock %} and {% block extrastyle %}{% endblock %} in my base.html.  Then in any specific template that extends base or base_site, I can fill in those blocks with {% block extrastyle %}<script ... ></script>{% endblock %}.  Otherwise it will be served up blank.

Hope that helps,

  -- Scott

Erwin Elling

unread,
Sep 8, 2008, 8:27:42 AM9/8/08
to Django users
Instead of using an "extra" head, you could do this:

Put your main css/js declaration inside a block in your base.html:
{% block headstuff %} blabla {% endblock %}

In templates that extend base.html and you want to add to this, do as
follows:
{% block headstuff %} {{block.super}} more bla {% endblock %}

The rendered block will contact "blabla more bla".

Good luck,
Erwin
Reply all
Reply to author
Forward
0 new messages