djangodjango-templatesdjango-template-filters

Return single comma from Django template yesno filter


Is it possible to make the yesno filter return a single comma (",") on true as it is also used as delimiter ({{foo.bar|yesno:"yup,nope"}} ~> instead of "yup" return a comma)?


Solution

  • Just as an alternative to the custom filter solution already suggested, you could HTML-encode the comma like so:

    {{ foo.bar|yesno:",,nope"|safe }}
    

    If you're reusing this logic in many templates then a filter is definitely more maintainable.