I need to change any ' to \' when displaying a value in my template.
so eg {{ account.company_name|escapejs }} should replace any ' with \'
how do I do that ?
thanks Thomas
** UPDATED CODE BASED ON COMMENTS BELOW **
My directory-structure is like this:
myapp/
....
templatetags/
__init__.py
replace.py
Replace.py contains:
from django import template
register = template.Library()
from django.template import defaultfilters
@register.filter
@defaultfilters.stringfilter
def replace(value, args=","):
try:
old, new = args.split(',')
return value.replace(old, new)
except ValueError:
return value
and in settings.py I have
INSTALLED_APPS = ( 'myapp' )
In my template I try to load the customtag like this:
{% from replace load replace %}
and it is used like this
company_name='{{ account.company_name|escapejs|replace:"',\'" }}'
I then received this error:
TemplateSyntaxError: Invalid block tag: 'from'
and then changed to
{% load replace %} instead of {% from replace load replace %}
but then I get this error:
TemplateSyntaxError: 'replace' is not a valid tag library: Template library replace not found, tried google.appengine._internal.django.templatetags.replace
Any help is appreciated