如何在使用“包含”模板标记的同时传递布尔关键字参数

时间:2022-09-19 17:57:11
{% include "example.html" with name="John" hide_last_name=True %}

Basically, I am trying to include "example.html" as a sub-template in my main template. Additional context is provided with the mean of passing the keyword arguments name and hide_last_name. While the django template system has no trouble recognize name, it somehow just can't recognize hide_last_name. I suspect the use of boolean keyword argument in Include tag is now allowed but then I can't find anywhere in the official docs mentions that. Please help out. Thanks.

基本上,我正在尝试包含“示例”。html作为我主模板中的子模板。附加的上下文提供了传递关键字参数名称和hide_last_name的方法。虽然django模板系统可以很容易地识别名称,但是它却无法识别hide_last_name。我怀疑现在允许在Include标记中使用布尔关键字参数,但我在官方文档中找不到任何地方提到这一点。请帮忙。谢谢。

3 个解决方案

#1


11  

Django template would treat the True as a variable and try to find it in context.
You could either use non-empty string to represent the true value or assign the true value to the True in context, for example through TEMPLATE_CONTEXT_PROCESSORS:

Django模板可以将True视为一个变量,并尝试在上下文中找到它。您可以使用非空字符串来表示真实值,或者在上下文中将真实值赋给真实值,例如通过template_context_processor:

def common_vars(request):
    return {
        'True': True,
        'False': False,
        'newline': '\n',
        ...
    }

#2


8  

For Django <= 1.4.x

Django < = 1.4.x

As said before, Django tries to find a variable named "True". The simplest way to handle this is to use an integer value, which will not be evaluated.

如前所述,Django试图找到一个名为“True”的变量。处理此问题的最简单方法是使用一个不进行计算的整数值。

You could write in the includer template

您可以在包含模板中写入

{% include "example.html" with show_last_name=1 %}

and in the included template

在包含的模板中

John
{% if show_last_name %}
    Doe
{% endif %}

For Django >= 1.5

Django > = 1.5

You can use True and False in templates, so this is no longer a issue

您可以在模板中使用True和False,因此这不再是个问题

#3


2  

In django 1.5 you can use True in django templates as per their release notes.

在django 1.5中,您可以根据其发行说明在django模板中使用True。

And if you are working on earlier versions you would have to go for what @okm suggested!

如果您正在开发早期版本,那么您必须按照@okm的建议进行操作!

#1


11  

Django template would treat the True as a variable and try to find it in context.
You could either use non-empty string to represent the true value or assign the true value to the True in context, for example through TEMPLATE_CONTEXT_PROCESSORS:

Django模板可以将True视为一个变量,并尝试在上下文中找到它。您可以使用非空字符串来表示真实值,或者在上下文中将真实值赋给真实值,例如通过template_context_processor:

def common_vars(request):
    return {
        'True': True,
        'False': False,
        'newline': '\n',
        ...
    }

#2


8  

For Django <= 1.4.x

Django < = 1.4.x

As said before, Django tries to find a variable named "True". The simplest way to handle this is to use an integer value, which will not be evaluated.

如前所述,Django试图找到一个名为“True”的变量。处理此问题的最简单方法是使用一个不进行计算的整数值。

You could write in the includer template

您可以在包含模板中写入

{% include "example.html" with show_last_name=1 %}

and in the included template

在包含的模板中

John
{% if show_last_name %}
    Doe
{% endif %}

For Django >= 1.5

Django > = 1.5

You can use True and False in templates, so this is no longer a issue

您可以在模板中使用True和False,因此这不再是个问题

#3


2  

In django 1.5 you can use True in django templates as per their release notes.

在django 1.5中,您可以根据其发行说明在django模板中使用True。

And if you are working on earlier versions you would have to go for what @okm suggested!

如果您正在开发早期版本,那么您必须按照@okm的建议进行操作!