如何覆盖Django基本模板 [英] How to override Django base template
问题描述
我需要在Django应用程序中扩展页面的基本模板,但是我需要从该新页面中排除一些HTML元素。删除它们的最佳方法是什么?我是否只使用JQuery删除元素?
I need to extend the base template for a page in my Django application, but there are a few HTML elements that I need to exclude from this new page that is in the base. What is the best way to remove them? Do I just delete the elements with JQuery?
推荐答案
您可以使用模板块来实现这一点。例如,在base.html中,将HTML元素括在命名块中:
You can use template blocks to achieve this. For example, in base.html, surround the HTML elements in a named block:
{% block a_unique_name %}<div>This is only relevant in base.html</div>{% endblock %}
仅当没有其他模板覆盖该块时,才使用该块。您可以像这样在子模板中覆盖它:
The HTML in the block will be used only if no other template overrides it. You can override it in your sub-template like this:
{% extend base.html %}
{% block a_unique_name %}{% endblock %}
现在将使用并将使用子模板中的值覆盖base.html中的默认值。
Now the value from sub-template will be used and will override the default value from base.html.
这篇关于如何覆盖Django基本模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!