Defining “global variable” in Django templates

后端 未结 5 902
情书的邮戳
情书的邮戳 2020-12-16 04:52

I\'m doing something like:

{% extends \'base.html\' %}
{% url myapp.views.dashboard object as object_url %}
{% block sidebar %}
... {{ object_url }} ...
{% e         


        
5条回答
  •  眼角桃花
    2020-12-16 05:26

    Well, this is kind of abusive of template inheritance, but you could use {{block.super}} to put object_url into your blocks.

    In other words, in your mid-level template do:

    {% block sidebar %}{{ object_url }}{% endblock %}
    {% block content %}{{ object_url }}{% endblock %}
    

    And then in your block templates use:

    {% block sidebar %}
    ... {{ block.super }}...
    {% endblock %}
    

    It's not a great idea because it prevents you from putting anything besides {{ object_url }} into your block... but it works. Just don't tell anyone you got it from me!

提交回复
热议问题