Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

python - django.template.exceptions.TemplateSyntaxError: Could not parse some characters: |{{b.count}}||rangef

I'm trying to loop in a Django template by using a custom filter, but for some reason I receive this error:

django.template.exceptions.TemplateSyntaxError: Could not parse some characters: |{{b.count}}||rangef

myfilters.py

@register.filter(name='rangef')
def rangef(number):
    return range(number)

index.html

<div class="container">
{% for i in {{b.count}}|rangef %}
    <p>Some text</p>
{% endfor %}
</div>

Important to mention that b.count is an Integer field. Appreciate your help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The custom filter can be used with the following syntax:

{{ b.count | rangef }}

Note that the filter part is inside the curly braces since this will apply the filter rangef to the value on the left, b.count.

Note that filters are not supported within for loops. Consider changing your model so that it exposes the filtered value directly, e.g.

def b_range(self):
    return range(self.b.count)
{% for i in b_range %}
…
{% endfor %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...