How to pass dynamic value in a slice using a for loop
Hi I have a list of objects and want to pass a dynamic variable:
{% for item in feed_item.feed_comment.all|slice:"6:10" %}
{% endfor %}
The above code works fine, but when I pass a dynamic value in the slice, it throws an error.
{% for item in feed_item.feed_comment.all|slice:feed_item.feed_comment.count:perpagecomment %}
{% endfor %}
Mistake
TemplateSyntaxError at /home/feed/
'for' statements should use the format 'for x in y': for item in feed_item.feed_comment.all|slice: feed_item.feed_comment.count
thank
+3
source to share
1 answer
Sorry, but as far as I know this is not possible. You can create your own snippet or even create a variable like this. I think it will work.
(view)
feed_comments = feed_comment.objects.all()
feed_comment = feed_comment.objects.count()
your_variable = "0:%d" % (feed_comment)
(template)
{% for feed in feed_comments|slice:your_variable %}
0
source to share