Django: construct value of {% include %} tag dynamically?

后端 未结 3 1055
傲寒
傲寒 2021-02-18 13:37

I\'d like to use an {% include page.html %} tag in my Django template, and construct the value of page.html dynamically.

Is there any way to do

3条回答
  •  悲哀的现实
    2021-02-18 14:12

    From the docs:

    The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.

    So construct a variable in your view and pass it to your template via the context, say as 'modeTemplate': "page_"+mode+".html". Then do:

    {% include modeTemplate %}
    

    Assuming 'mode' is a python variable in your view code.

提交回复
热议问题