Python 静态文件和django模板

Python 静态文件和django模板,python,css,django,dom,django-templates,Python,Css,Django,Dom,Django Templates,我想把我所有的链接标记放在中 但是,当我通过内置的include标记包含共享模板时,我不知道如何呈现我的DOM的head中的所有link标记。因此,我的链接标记将在我碰巧包含共享模板的任何地方呈现。为了更好地说明我的问题,我在下面添加了代码 布局: <html> <head> {% block references %}{% endblock %} </head> <body> {% block content %}{% end

我想把我所有的
链接
标记放在

但是,当我通过内置的
include
标记包含共享模板时,我不知道如何呈现我的DOM的
head
中的所有
link
标记。因此,我的
链接
标记将在我碰巧包含共享模板的任何地方呈现。为了更好地说明我的问题,我在下面添加了代码

布局:

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

{%block引用%}{%endblock%}
{%block content%}{%endblock%}
使用模板扩展布局:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}
...
{% include "mySharedTemplate.html" %}
...
{% 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 %}
{%extends“layout.html”%}
{%load staticfiles%}
{%block references%}
{%endblock%}
...
{%include“mySharedTemplate.html”%}
...
共享模板。注意,此模板在我的几个但不是所有模板之间共享:

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

在使用共享模板时,是否有办法将我所有的
链接
标记放在我的DOM的
头部
?有没有完全不同或更好的方法?我的第一个django项目已经进行了一周,所以即使是对基本特性的建议也可能对我有所帮助

我想你在找
{{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>
<html>
<head>
    {% block references %}{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>
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>
<html>
<head>
    {% block references %}{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

{%block引用%}{%endblock%}
{%block content%}{%endblock%}
layout-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%}
{%endblock%}
没有共享模板的模板:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}
...
{% include "mySharedTemplate.html" %}
...
{% 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 %}
{%extends“layout.html”%}
{%load staticfiles%}
{%block references%}
{%endblock%}
具有共享模板的模板:

{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
    <link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% endblock %}
...
{% include "mySharedTemplate.html" %}
...
{% 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 %}
{%extends“布局共享css.html”%}
{%load staticfiles%}
{%block references%}
{{block.super}}
{%endblock%}

我找到了一种很好的方法。我对它不太满意。我发现我可以使用简单的
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 %}
{%extends“layout.html”%}
{%load staticfiles%}
{%block references%}
{%include“mySharedTemplate.html”和references=“True”%}
{%endblock%}
...
{%include“mySharedTemplate.html”和content=“True”%}
...
共享模板:

{% if references %}
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static "mySharedTemplateStylesheet.css" %}" type="text/css">
{% endif %}
{% if content %}
    ...
{% endif %}
{%if references%}
{%load staticfiles%}
{%endif%}
{%if内容%}
...
{%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" %}
...
{%extends“layout.html”%}
{%load staticfiles%}
{%block references%}
{%include“mySharedTemplate.html”和references=“True”%}
{%include“mySharedTemplate2.html”和references=“True”%}
{%endblock%}
...
{%include“mySharedTemplate.html”和content=“True”%}
{%include“mySharedTemplate2.html”和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" %}
...
{%extends“layout.html”%}
{%load staticfiles%}
{%block references%}
{%include“mySharedTemplate2.html”和references=“True”%}
{%include“mySharedTemplate3.html”和references=“True”%}
{%include“mySharedTemplate4.html”和references=“True”%}
{%endblock%}
...
{%include“mySharedTemplate4.html”和content=“True”%}
{%include“mySharedTemplate3.html”和content=“True”%}
{%include“mySharedTemplate2.html”和content=“True”%}
...

请注意,模板2和模板3可以以适合它们的方式使用共享模板,而不需要太多的锅炉板代码。

使用
逐字标记来停止模板引擎,将您的标记解释为他的标记

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

我不想这样做,因为我的所有页面都使用“共享模板”。@steaks,“共享模板”使用了多少次?如果经常使用,请尝试Victor Castillo Torres的答案。@steaks,我更新了“共享模板”很少使用时的代码。您的答案将共享模板的链接和标记放在引用块中。这是否会导致共享模板呈现错误,因为标记将呈现在DOM的
头部
。这就是我所说的省略号。请参阅
我不希望将
mySharedTemplateStylesheet.css
放在我的布局中,因为并非所有模板都使用共享模板标记。我宁愿将
mySharedTemplateStylesheet.css
包含在template.html中,而不是使用
block.super
。它们基本上都做相同的事情
block.super
只是混淆代码,并使layout.html中的
引用
块包含它不需要的代码。我自己添加了一个答案。如果您认为是这样,请解释为什么您认为您的解决方案或其他解决方案更好。我很乐意参与!我讨厌提供我自己的答案:)。共享模板分为两部分如何:一部分用于参考,另一部分用于Content。@falsetru我想我试图通过将它们放在单独的
if
块中来实现这一点。有更好的办法吗?我可以用
include
标记包含特定的
块吗?我在回答中解释了为什么我没有这样做。注意,我可以通过