2

linkすべてのタグを に入れたい<head>

ただし、組み込みのタグを介して共有テンプレートを含める場合、DOMlinkのすべてのタグをレンダリングする方法がわかりません。そのため、共有テンプレートを含めると、タグがレンダリングされます。私の問題をよりよく説明するために、以下のコードを追加しました。headincludelink

レイアウト:

<html>
<head>
    {% block references %}{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

テンプレートを使用してレイアウトを拡張する:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}
...
{% include "mySharedTemplate.html" %}
...

共有テンプレート。このテンプレートはいくつかのテンプレートで共有されていますが、すべてのテンプレートで共有されているわけではありません:

{% load staticfiles %}
<link rel="stylesheet" href="{% static "mySharedTemplateStylesheet.css" %}" type="text/css">
...

共有テンプレートを使用しているときに、すべてのlinkタグをDOM に配置する方法はありますか? headこれを行うための完全に異なる方法またはより良い方法はありますか? 私は最初の django プロジェクトを開始してから 1 週間が経ちました。基本的な機能の提案でも役立つかもしれません。

4

4 に答える 4

1

私はあなたが探していると思います{{block.super}}

たとえば、Layout.html:

<html>
<head>
{% load staticfiles %}
{% block references %}
   <link rel="stylesheet" href="{% static "mySharedTemplateStylesheet.css" %}" type="text/css">

{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

および Template.html で:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    {{block.super}}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}

mySharedTemplateStylesheet.cssすべてのページにを使用したくない場合は{{block.super}}、Template2.html のように使用しないだけです。

 {% extends "layout.html" %}
    {% load staticfiles %}
    {% block references %}
        <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
    {% endblock %}
于 2013-06-21T03:37:17.880 に答える
1

レイアウト.html:

<html>
<head>
    {% block references %}{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

レイアウト-with-shared-css.html:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}

共有テンプレートのないテンプレート:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}

テンプレートと共有テンプレート:

{% extends "layout-shared-css.html" %}
{% load staticfiles %}
{% block references %}
    {{ block.super }}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}
于 2013-06-21T03:39:40.283 に答える
0

これを行うハックな方法を見つけました。私はそれにとても満足していません。シンプルなブロックを使用して、テンプレートのどのセクションをタグifでレンダリングするかを切り替えることができることがわかりました。includeこれにより、参照とコンテンツを別々に含めることができます。(注意してください。参照とコンテンツを別々のファイルに分けることで、この問題を解決できます。しかし、それはこの解決策よりも退屈なようです。)

共有テンプレートを他のテンプレートから分離できるため、このソリューションは現在の回答よりも優れています。このモジュラー デザインを維持することは、組み合わせて使用​​できる機能を使用する場合に重要です (これは、共有テンプレートで行いたいことです)。

テンプレート:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
    {% include "mySharedTemplate.html" with references="True" %}
{% endblock %}
...
{% include "mySharedTemplate.html" with content="True" %}
...

共有テンプレート:

{% if references %}
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static "mySharedTemplateStylesheet.css" %}" type="text/css">
{% endif %}
{% if content %}
    ...
{% endif %}

私のモジュラー設計が重要であると考える理由を説明するために:

多数の共有テンプレートと多数の通常のテンプレートがあり、それぞれが共有テンプレートをさまざまな方法で使用しているとします。私のモジュラー方式により、通常のテンプレートが共有テンプレートと最も適した柔軟な方法で簡単に連携できるようになります。

テンプレート 2:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
    {% include "mySharedTemplate.html" with references="True" %}
    {% include "mySharedTemplate2.html" with references="True" %}
{% endblock %}
...
{% include "mySharedTemplate.html" with content="True" %}
{% include "mySharedTemplate2.html" with content="True" %}
...

テンプレート 3:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
    {% include "mySharedTemplate2.html" with references="True" %}
    {% include "mySharedTemplate3.html" with references="True" %}
    {% include "mySharedTemplate4.html" with references="True" %}
{% endblock %}
...
{% include "mySharedTemplate4.html" with content="True" %}
{% include "mySharedTemplate3.html" with content="True" %}
{% include "mySharedTemplate2.html" with content="True" %}
...

テンプレート 2 とテンプレート 3 では、ボイラー プレート コードをあまり使わなくても、共有テンプレートを適切な方法で使用できることに注意してください。

于 2013-06-21T04:32:18.487 に答える
0

タグを使用verbatimしてテンプレート エンジンを停止し、タグを彼のタグとして解釈します。

{% verbatim %}
    {{if dying}}Still alive.{{/if}}
{% endverbatim %}

Django と Chartjs テンプレートの競合

于 2017-12-04T13:59:11.490 に答える