django中是不能直接调用函数的,可以通过自定义filter来实现
#template_filters.py @register.filter def template_args(instance, arg): """ stores the arguments in a separate instance attribute """ if not hasattr(instance, "_TemplateArgs"): setattr(instance, "_TemplateArgs", []) instance._TemplateArgs.append(arg) return instance @register.filter def template_method(instance, method): """ retrieves the arguments if any and calls the method """ method = getattr(instance, method) if hasattr(instance, "_TemplateArgs"): to_return = method(*instance._TemplateArgs) delattr(instance, '_TemplateArgs') return to_return return method() # 在模版里面按照下面的方法调用 {{ instance|template_args:"value1"|template_args:"value2"|template_args:"value3"|template_method:"test_template_call" }} # 输出结果 value1, value2, value3