Calling block inside an if condition: django template

前端 未结 1 1739
梦谈多话
梦谈多话 2021-02-05 02:09

I have been trying to call a block inside an if condition in django template.

I have a base template. I have many other templates that extend the base templ

1条回答
  •  礼貌的吻别
    2021-02-05 02:36

    You haven't made a mistake - template blocks are included regardless of any conditionals around them. You can see this from this line of the ExtendsNode class of django/template/loader_tags.py in the Django source code:

    self.blocks = {n.name: n for n in nodelist.get_nodes_by_type(BlockNode)]}
    

    When the {% extends %} is being rendered, it fetches all block nodes from the template directly and stores them for rendering when the parent comes across those blocks. Whether those blocks in the child were inside conditionals or not isn't considered.

    0 讨论(0)
提交回复
热议问题