Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django模板内SafeUnicode的长度_Python_Django - Fatal编程技术网

Python django模板内SafeUnicode的长度

Python django模板内SafeUnicode的长度,python,django,Python,Django,我注意到,如果将一个名为some_copy的变量(包含unicode字符串)传递到Django模板中: some_copy <type 'unicode'> some_copy|length <type 'int'> some_copy|safe <class 'django.utils.safestring.SafeUnicode'> some_copy|safe|length <class 'django.utils.safestring.Safe

我注意到,如果将一个名为some_copy的变量(包含unicode字符串)传递到Django模板中:

some_copy <type 'unicode'>
some_copy|length <type 'int'> 
some_copy|safe <class 'django.utils.safestring.SafeUnicode'>
some_copy|safe|length <class 'django.utils.safestring.SafeString'>  
一些拷贝
一些拷贝长度
一些拷贝是安全的
一些拷贝|安全|长度
我注意到长度过滤器只是在输入上调用
len()
,所以测试了在终端上导入
django.utils.safestring.SafeUnicode
,当调用
len()
时,它似乎只返回
。关于安全过滤器如何与其他过滤器交互,这里有什么奇怪的地方吗?我知道有注释建议在安全后不要使用其他过滤器,因为它们可能会使输出不那么安全。此外,这段代码产生了一个bug,不再包含对
safe | length
的调用,但我很好奇到底发生了什么。

源代码:

@register.filter(is_safe=True)
def length(value):
    """Returns the length of the value - useful for lists."""
    try:
        return len(value)
    except (ValueError, TypeError):
        return ''
@register.filter(is_safe=True)
告诉Django通过调用
mark_safe
length
函数的输出转换为
SaveData
子类实例。对于整数输入,
mark_safe
返回一个
SafeString
实例