Django: Overriding AND extending an app template

前端 未结 6 2032
北荒
北荒 2020-12-02 18:05

If you want to override a template coming with an app in django (in app/templates/app/) you create a template of the same name in another directory, which the template loade

6条回答
  •  日久生厌
    2020-12-02 18:47

    Django 1.9 and later has this feature:

    Django template loaders can now extend templates recursively.

    So you can have a file from app1 with contents like this:

    app1/templates/example/foobar.html:

    I'm from app1

    {% block main %}{% endblock %}

    And you can extend it with a file from app2 (notice that the template name example/foobar.html is the same):

    app2/templates/example/foobar.html:

    {% extends "example/foobar.html" %}
    {% block main %}
      

    I'm from app2

    {% endblock %}

    The end-result should be:

    I'm from app1

    I'm from app2

提交回复
热议问题