Reputation: 1245
I have been trying to write my 1st django custom template tag, by reading the django docs.
The custom template tag I have written contains a conditional if elseif else condition.
The result is always returning the else condition.
Here is my custom template tag code:
@register.filter(name='replace_date_separator')
def replace_date_separator(value, arg):
if arg is 'fr-CA':
return value.replace("/", '-')
elif arg is 'de' or arg is 'pl' or arg is 'ru':
return value.replace("/", '.')
else:
return value.replace("/", '*')
Here is my template tag:
{{ voluntary_detail.voluntary_finish_date|date:'m/Y'|replace_date_separator:voluntary_detail.language_version.language_code }}
The voluntary_detail.language_version.language_code above is the two letter language code - ru, de, en, pl, etc.
Upvotes: 0
Views: 563
Reputation: 77023
You are using the is
keyword, which checks for object identity match, and hence, always fails.
Instead use ==
to check for equality, and do:
@register.filter(name='replace_date_separator')
def replace_date_separator(value, arg):
if arg == 'fr-CA':
return value.replace("/", '-')
elif arg == 'de' or arg == 'pl' or arg == 'ru':
return value.replace("/", '.')
else:
return value.replace("/", '*')
As an aside, you can simplify the elif
statement to elif arg in ('de', 'pl', 'ru'):
Upvotes: 4