[Answer]-How do I replace string in django when displaying a value

1👍

Use the addslashes built-in filter:

From the documentation’s example:

{{ value|addslashes }}

If value is "I'm using Django", the output will be "I\'m using Django".

0👍

You can write your own filter:

@register.filter
@defaultfilters.stringfilter
def replace(value, args=","):
    try:
        old, new = args.split(',')
        return value.replace(old, new)
    except ValueError:
        return value

Then in your template:

{% load replace from mycustomtags %}

{{ account.company_name|escapejs|replace:"',\'" }}

(I haven’t tested)

Leave a comment