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_STATIC。また、base.html スイッチにもテーマ変数が設定されています。

もちろん、始める前からグーグルで検索しましたが、良い検索用語を見つけるのが難しいことがわかりました(良いリソースがたくさんあると思います)

4

1 に答える 1

5

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

theme + "/base.html"get_templateを呼び出す前に、メインコントローラーのようにコンテキスト変数「base」を設定します

于 2011-02-23T13:46:00.977 に答える