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

发布于 2024-10-19 02:43:28 字数 925 浏览 1 评论 0原文

在我的 django 网站中,我想为我们最好的客户和老板提供几个主题。所以我很快创建了以下内容。 - 我很高兴我能顺利地展示它,但我想通过我向您提出的好的解决方案来消除一些肮脏的黑客行为。

这是我的 hack

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 中。它会针对每个请求进行检查,并在模板中根据 /mypathto/static/d0 resp 检查 PREFIX_STATIC 上下文变量。 +d1 +d2 当然,我必须发明一个 COMMON_STATIC var。并且主题变量也为 base.html 开关设置。

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

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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳男子 2024-10-26 02:43:28

来自 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).

我在主控制器中将模板更改为 do

{% extends base %} 

而不是并将上下文 var "base" 设置为 theme + "/base.html"

{% extends "base.html" %}

因此,在调用 get_template 之前,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文