Is there a way to pass a variable to an 'extended' template in Django?

后端 未结 2 1189
星月不相逢
星月不相逢 2021-02-03 22:31

I want to add some flexibility to my layout template, but I can\'t find any way to do so.

I\'m looking for a way to extend my layout template with variable

2条回答
  •  迷失自我
    2021-02-03 22:51

    I suspect that block is what you are looking for in the first place.

    Form your block inside the base template like this:

    {% block sidebar_wrapper %}
        {% if sidebar %}
        
    {% block sidebar %}{% endblock %}
    {% endif %} {% endblock sidebar_wrapper%}

    And on your child template:

    {% extends 'layout.html' %}
    {% block sidebar_wrapper %}
        {% with sidebar=True sidebar_width=4 %}
            {{ block.super }}
        {% endwith%}
    {% endblock sidebar_wrapper%}
    

提交回复
热议问题