【问题标题】:theme switching, template and css file layout on a django sitedjango 站点上的主题切换、模板和 css 文件布局
【发布时间】:2011-02-23 11:16:55
【问题描述】:

在我的 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_STATIC var。并且为 base.html 开关设置了主题变量。

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

【问题讨论】:

  • 我承认我没有检查块是否已填满,它看起来不像那样工作。我的问题是:如何让 django 在几个 base.html 之间进行选择

标签: django theming


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多