0

I'd like to find a way to replace a part of a string: I'd like to remove everything after a substring, like ",".

Example: If my input string is "Hello, world!", the output should be "Hello".

I was surprised to find a join filter and not a split one.

Do I need to write a custom filter or can I do it with a built-in filter (I can't find it)? Thank you.

David Dahan
  • 10,576
  • 11
  • 64
  • 137

1 Answers1

0

Based on your links, I created a custom template tag myself, it perfectly works, here is the code:

// In a custom template file (see doc)

from django import template

register = template.Library()

@register.filter
def del_after_char(input_str, char):
    try:
        return input_str.split(char)[0]
    except: # Do nothin if an error occurs
        return input_str
David Dahan
  • 10,576
  • 11
  • 64
  • 137