18👍
You could use slice
:
{% for obj in list|slice:":5" %}
...
{% endfor %}
<a href="" onclick="unhide()">Show hidden</a>
<div id="hidden">
{% for obj in list|slice:"5:" %}
...
{% endfor %}
</div>
7👍
Struggling with template limitations can often be a sign that life might be simpler if you did more work in your view:
context = {
'visible_list': mylist[:5],
'hidden_list': mylist[5:]
}
I’m not saying this is neccesarily any better than the slice filter solution above but it’s often worth considering passing your templates a context object with everything set up nicely. It allows you to adapt more easily should the logic get even more complex later on.
Presentation logic sits quite nicely in a view. I don’t particularly see it as ‘pollution’.
- [Django]-Form loses ability to send POST requests after 2 ajax updates
- [Django]-How to fix "Failed to restart gunicorn.service: Unit gunicorn.socket not found." error?
- [Django]-Django Forms for generic relationships. How to include them?
- [Django]-Is it possible to redirect the python output to web by using 'sys.stdout' command?
2👍
Sure, for example you can do the loop twice and within it use forloop.counter
in suitable if
constructs — the first time to only do stuff when it’s <= 5
, the second time only when it’s > 5
(remember it’s 1-based — if you want a 0-based one, use forloop.counter0
instead).
- [Django]-Django: How to avoid duplicated html id for showing field twice in the same form?
- [Django]-Implement get_serializer in generic list view (generics.ListCreateAPIView) in Django REST Framework
- [Django]-Changing settings for the tests in django (search engine for haystack to be specific)
- [Django]-Django-uploadify-s3 and HTTP 403 Error