3👍
✅
you could write a template filter that runs a string through json decode.
{% for image_path in data.image_paths|your_custom_json_decode_filter %}
{{ image_path }}
{% endfor %}
This is not a good idea though, Why don’t you do this in your view though?
0👍
A template filter is your best option since there is no built in template tag to evaluate a string. If you want to convert it in the template that’s your best option. However it wouldn’t make that much sense building a template filter in python when you could just modify your data before sending it to the template. Either way you are going to have to do something in python.
If you plan on using the template filter here is an example:
@register.filter # register the template filter with django
def get_string_as_list(value): # Only one argument.
"""Evaluate a string if it contains []"""
if '[' and ']' in value:
return eval(value)
And then in your template you would want to loop over the key,values in your dictionary and pass the values to you custom template filter as so:
{% for image_path in data.image_paths|get_string_as_list %}
{{ image_path }}
{% endfor %}
- [Django]-Django use value of template variable as part of another variable name
- [Django]-Alternate URL router for Django
- [Django]-Deactivate user access or delete it?
- [Django]-Get first object from Django queryset
Source:stackexchange.com