2

在我的 django 站点中,我想为我们最好的客户和老板提供几个主题。所以我很快创建了以下内容。- 我很高兴我能相处融洽,但有一些肮脏的技巧我想用我要求的很好的解决方案来消除。

这是我的黑客

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 %}

然后我将所有常见 css 和 js 的子目录保存在 MEDIA 目录中

并创建了子目录

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

我的控制器有一种切换设计的方法,当前一个存储在 cookie 中。它会根据每个请求和模板中的PREFIX_STATIC上下文变量进行检查/mypathto/static/d0 resp. +d1 +d2 ,当然我必须发明一个COMMON_STATICvar。并且为 base.html 开关设置了主题变量。

当然,我什至在开始之前就搜索了谷歌,但发现很难找到好的搜索词(因为我希望有很多好的资源)

4

1 回答 1

5

来自 loader_tags.py Include_Node do_extends 文档:

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" %}

theme + "/base.html"并在我调用 get_template 之前在我的主控制器中设置上下文变量“base”

于 2011-02-23T13:46:00.977 回答