Python 如何覆盖Django中包含的模板块?
假设我有这样的结构:Python 如何覆盖Django中包含的模板块?,python,django,Python,Django,假设我有这样的结构: {# base.html #} {% block content %}{% endblock %} {# page.html #} {% extends "base.html" %} {% block content %} {% include "snippet.html" %} {# and I also want somehow to redefine {% block snippet_content %} of snippet here #} {% endblock
{# base.html #}
{% block content %}{% endblock %}
{# page.html #}
{% extends "base.html" %}
{% block content %}
{% include "snippet.html" %}
{# and I also want somehow to redefine {% block snippet_content %} of snippet here #}
{% endblock %}
{# snippet.html #}
<bells_and_whistles>
{% block snippet_content %}{% endblock %}
</bells_and_whistles>
{base.html}
{%block content%}{%endblock%}
{#page.html}
{%extends“base.html”%}
{%block content%}
{%include“snippet.html”%}
{#我还想以某种方式重新定义{%block snippet_content%}这里的snippet}
{%endblock%}
{#snippet.html}
{%block snippet_content%}{%endblock%}
我希望代码是自我解释的
有没有一种优雅的方法可以做到这一点呢?恐怕你想用的方法是不可能做到的 你的选择是:
modified_snippet.html
继承自snippet.html
并重写该块并将其包含在内snippet\u content
块更改为{{snippet\u content}}
变量,并使用{%include“snippet.html”和snippet\u content=“My content”%}
传递其内容。当然,这种方法是非常有限的恐怕这不可能以你想要的方式来做 你的选择是:
modified_snippet.html
继承自snippet.html
并重写该块并将其包含在内snippet\u content
块更改为{{snippet\u content}}
变量,并使用{%include“snippet.html”和snippet\u content=“My content”%}
传递其内容。当然,这种方法是非常有限的