2011-02-23 57 views
1

在我的django網站中,我想爲我們最好的客戶和老闆提供幾個主題。所以我很快創建了以下內容。 - 我很高興我能夠介紹它,但有一些骯髒的黑客我想要用我想問你的好解決方案來消除。django網站上的主題切換,模板和css文件佈局

這裏是我的黑客

base.html文件說(小心! - 醜)

{% ifequal theme "0" %} 
    {% include "base_d0.html" %} 
{% endifequal %} 

{% ifequal theme "1" %} 
    {% include "base_d1.html" %} 
{% endifequal %} 

{% ifequal theme "2" %} 
    {% include "base_d2.html" %} 
{% endifequal %} 

然後我一直在MEDIA目錄的子目錄所有常見CSS和JS

和創建子目錄

static/ 
    d0/  (all theme 0 stuff) 
    css/ 
    js/ 
    d1/  (all theme 1 stuff) 
    css/ 
    js/ 
    ... 
    css/ 
    (all common css) 
    js/ 
    (all common js) 

我的控制器有一個方法來切換設計,當前的一個存儲在 一塊餅乾。它被檢查每個請求和模板PREFIX_STATIC上下文變量相應/mypathto/static/d0 resp. +d1 +d2,當然我不得不發明COMMON_STATIC變種。主題var也爲base.html開關設置。

當然,我甚至開始前用Google搜索,但發現很難找到好的搜索字詞(如我期望有很多很好的資源)

+0

我承認,如果塊填充我還沒有檢查之前,它不工作,這樣看來。我的問題是:如何讓Django在幾個base.html之間進行選擇 – groovehunter 2011-02-23 12:27:42

回答

3

從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). 

所以我改變了我的模板做

{% extends base %} 

,而不是

{% extends "base.html" %} 

,並設置背景VAR「基地」爲theme + "/base.html"在我的主控制器我打電話get_template