我想给我的布局模板增加一些灵活性,但我找不到任何方法.
我正在寻找一种用变量来extend
布局模板的方法,即在模板树中向上传递变量,而不是向下传递变量.
# views.py
def my_view_func(request):
return render(request, "child.html")
# child.html
{% extends 'layout.html' with show_sidebar=True sidebar_width_class="width_4" %}
<div>Templates stuff here</div>
# layout.html
{% if show_sidebar %}
<div class="{{ sidebar_width_class }}">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
我必须维护四个模板,并在几行代码中有所不同.例如,我有两个侧栏宽度类不同的模板.我做错什么了吗?