dj.name.replace('&', 'and') 您不能使用带参数的方法来调用。您需要编写一个自定义过滤器。官方指南在这里:
https://docs.djangoproject.com/en/dev/howto/custom-template-
tags/
好的,这是我的示例,例如,在一个名为“ questions”的应用程序中,我想编写一个过滤器
to_and,该过滤器将字符串中的“&”替换为“ and”。
在/ project_name / questions / templatetags中,创建一个blank
__init__.py,
to_and.py其内容类似于:
from django import templateregister = template.Library()@register.filterdef to_and(value): return value.replace("&","and")在template中,使用:
{% load to_and %}那么您可以享受:
{{ string|to_and }}注意,目录名
templatetags和文件名
to_and.py不能是其他名称。



