django templates: include and extends

后端 未结 7 2022
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 10:37

I would like to provide the same content inside 2 different base files.

So I\'m trying to do this:

page1.html:

{% extends \"base1.html\" %}
         


        
7条回答
  •  醉话见心
    2020-12-07 11:24

    Edit 10th Dec 2015: As pointed out in the comments, ssi is deprecated since version 1.8. According to the documentation:

    This tag has been deprecated and will be removed in Django 1.10. Use the include tag instead.


    In my opinion, the right (best) answer to this question is the one from podshumok, as it explains why the behaviour of include when used along with inheritance.

    However, I was somewhat surprised that nobody mentioned the ssi tag provided by the Django templating system, which is specifically designed for inline including an external piece of text. Here, inline means the external text will not be interpreted, parsed or interpolated, but simply "copied" inside the calling template.

    Please, refer to the documentation for further details (be sure to check your appropriate version of Django in the selector at the lower right part of the page).

    https://docs.djangoproject.com/en/dev/ref/templates/builtins/#ssi

    From the documentation:

    ssi
    Outputs the contents of a given file into the page.
    Like a simple include tag, {% ssi %} includes the contents of another file
    – which must be specified using an absolute path – in the current page
    

    Beware also of the security implications of this technique and also of the required ALLOWED_INCLUDE_ROOTS define, which must be added to your settings files.

提交回复
热议问题