django templates: include and extends

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

    From Django docs:

    The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.

    So Django doesn't grab any blocks from your commondata.html and it doesn't know what to do with rendered html outside blocks.

    0 讨论(0)
  • 2020-12-07 11:15

    This should do the trick for you: put include tag inside of a block section.

    page1.html:

    {% extends "base1.html" %}
    
    {% block foo %}
       {% include "commondata.html" %}
    {% endblock %}
    

    page2.html:

    {% extends "base2.html" %}
    
    {% block bar %}
       {% include "commondata.html" %}
    {% endblock %}
    
    0 讨论(0)
  • 2020-12-07 11:16

    More info about why it wasn't working for me in case it helps future people:

    The reason why it wasn't working is that {% include %} in django doesn't like special characters like fancy apostrophe. The template data I was trying to include was pasted from word. I had to manually remove all of these special characters and then it included successfully.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-07 11:23

    You can't pull in blocks from an included file into a child template to override the parent template's blocks. However, you can specify a parent in a variable and have the base template specified in the context.

    From the documentation:

    {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

    Instead of separate "page1.html" and "page2.html", put {% extends base_template %} at the top of "commondata.html". And then in your view, define base_template to be either "base1.html" or "base2.html".

    0 讨论(0)
  • 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.

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