Python/Django - 模板中的 If 语句围绕扩展 [英] Python / Django - If statement in template around extends

查看:25
本文介绍了Python/Django - 模板中的 If 语句围绕扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个有条件地扩展另一个模板.基本上,当名为ajax"的变量为真时,我不想让模板扩展另一个.

I would like to have a template that extends another conditionally. Basically, when a variable called "ajax" is true I DO NOT want to have the template extend another.

{% if not ajax %}
    {% extends "/base.html" %}
{% endif %}

有什么线索吗?

推荐答案

你不能那样做.但是,您可以设置一个变量并使用它来选择要扩展的模板:

You cannot do it like that. You can however set a variable and use that to choose the template to extend:

{% extends my_template %}

然后在 python 代码中编写如下内容:

Then in python code you write something like:

if ajax:
    template_values['my_template'] = 'base_ajax.html'
else:
    template_values['my_template'] = 'base.html'

您可能希望参考文档了解更多信息信息.

You may wish to refer to the documentation for more information.

这篇关于Python/Django - 模板中的 If 语句围绕扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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