django 站点上的主题切换、模板和 css 文件布局

gro*_*ter 2 django theming

在我的 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 %}
Run Code Online (Sandbox Code Playgroud)

然后我将所有常见 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)
Run Code Online (Sandbox Code Playgroud)

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

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

gro*_*ter 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).
Run Code Online (Sandbox Code Playgroud)

所以我改变了我的模板来做

{% extends base %} 
Run Code Online (Sandbox Code Playgroud)

代替

{% extends "base.html" %}
Run Code Online (Sandbox Code Playgroud)

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