Django:动态构造{%include%}标记值 [英] Django: construct value of {% include %} tag dynamically?

查看:108
本文介绍了Django:动态构造{%include%}标记值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Django模板中使用 {%include page.html%} 标记,并构造页面的值。 html 动态

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

Django有没有办法做到这一点?

Is there any way to do this in Django?

这是一个伪代码示例:

{% include page_{{mode}}.html %}

谢谢!

推荐答案

文件:


模板名称可以是
变量或硬编码(引用)
字符串,在

引号中。

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

因此,在视图中构造一个变量,并通过上下文将其传递给您的模板说'modeTemplate':page _+ mode +。html。然后做:

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 %}

假设'mode'是您的视图代码中的一个python变量。

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

这篇关于Django:动态构造{%include%}标记值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆