问题
In my django site I want to offer several themes for our best clients and for the boss. So I quickly created the following. - I am glad i got along to present it but there are a few dirty hacks I want to eliminate with nice solutions I ask you for.
Here's my hack
base.html says (take care - ugly!)
{% ifequal theme "0" %}
{% include "base_d0.html" %}
{% endifequal %}
{% ifequal theme "1" %}
{% include "base_d1.html" %}
{% endifequal %}
{% ifequal theme "2" %}
{% include "base_d2.html" %}
{% endifequal %}
Then i kept in MEDIA dir the subdirs for all common css and js
and created subdirs
static/
d0/ ( all theme 0 stuff )
css/
js/
d1/ ( all theme 1 stuff )
css/
js/
...
css/
(all common css)
js/
(all common js)
My controller has a method to switch design, current one is stored in a cookie. It is checked on every request and in template a PREFIX_STATIC
context variable accordingly to /mypathto/static/d0 resp. +d1 +d2
and of course i had to invent a COMMON_STATIC
var. And the theme var is set too for the base.html switch.
Of course i googled even before starting but found it hard to find good search terms (as i expect there are many good resources )
回答1:
from loader_tags.py Include_Node do_extends doc:
This tag may be used in two ways: ``{% extends "base" %}`` (with quotes)
uses the literal value "base" as the name of the parent template to extend,
or ``{% extends variable %}`` uses the value of ``variable`` as either the
name of the parent template to extend (if it evaluates to a string) or as
the parent tempate itelf (if it evaluates to a Template object).
So i changed my templates to do
{% extends base %}
instead of
{% extends "base.html" %}
and set context var "base" as theme + "/base.html"
in my main controller before i call get_template
来源:https://stackoverflow.com/questions/5090149/theme-switching-template-and-css-file-layout-on-a-django-site