简体   繁体   中英

How can I make this custom filter work in django?

I am trying to make a custom template take to perform a split function on a form value where I am sending two arguements - the PK and the name .

EX.

  value="{{it.pk}} {{it.name}}" 

Values shown would be something like: 43 mark the 2nd

This is my code below:

   @register.filter_function
   def split(item,args):
       args = args.split(item)
       return args[1]

So in my template I want to put {{item|spit:" "}} to get the name

You have to join the rest of the text bits after the split:

@register.filter_function
def split(text, split_by):
    return split_by.join(text.split(split_by)[1:])

If I get your desire right:

>>> split('43 mark the 2nd', ' ')
'mark the 2nd'
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM