django templates: include and extends

后端 未结 7 2016
没有蜡笔的小新
没有蜡笔的小新 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:18

    When you use the extends template tag, you're saying that the current template extends another -- that it is a child template, dependent on a parent template. Django will look at your child template and use its content to populate the parent.

    Everything that you want to use in a child template should be within blocks, which Django uses to populate the parent. If you want use an include statement in that child template, you have to put it within a block, for Django to make sense of it. Otherwise it just doesn't make sense and Django doesn't know what to do with it.

    The Django documentation has a few really good examples of using blocks to replace blocks in the parent template.

    https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance

提交回复
热议问题