2
votes

I have a Django template that looks like this:

 <div id="objects">
                <b>Ingredients:</b>
                <small>
                {% for obj in result.object.list_objects|slice:":10" %}
                    .{{ obj }}
                {% endfor %}
                ......
                </small>
              </div>

I am trying to output a simple list but when I loop through these elements in the template, the output is unfortunately in single quotes for each object. I am new to Django templates and have tried using a few methods like safe and escape. But the single quotes remain. Is there a way in Django to strip out that specific character when loading the data?

2
(a) Have you written __str__ methods for your model? (b) Why are you passing result into your template? IMO you should slice and dice the list of objects in your view code and pass the list of ten objects you want directly to your template. (c) Is this your exact code? I suspect it's not, since your list_objects contains a typo. Please edit your question and paste exactly the code you are using. A single character can make a big difference.Chris
Also, please show the data itself.Daniel Roseman

2 Answers

5
votes

This question looks a bit dated, but wanted to toss in a simple solution. You can make a custom template tag and use the replace function.

Example:

@register.filter
def strip_double_quotes(quoted_string):
    return quoted_string.replace('"', '')

{{ my_quoted_string|strip_double_quotes }}

2
votes

You can use

                {% for obj in result.object.list_objects %}
                    {{ obj|slice:"1:-1" }}
                {% endfor %}