115👍
The easiest workaround (until the resetcycle patch gets fixed up and applied) is to use the built-in “divisibleby” filter with forloop.counter:
{% for entry in blog.entries %}
<div class="{% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}" id="{{ entry.id }}">
{{ entry.text }}
</div>
{% endfor %}
A little more verbose, but not hard to understand and it works great.
24👍
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#cycle
{% for o in some_list %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% endfor %}
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-Django-Admin: CharField as TextArea
- [Django]-Update all models at once in Django
3👍
Give up and use Jinja2 Template System
I gave up on django template language, it’s very restricted in what you can do with it.
Jinja2 uses the same syntax that the django template uses, but adds many enhancements over it.
EDIT/NOTE ( I know it sounds like a big switch for just a minor issue, but in reality I bet you always find yourself fighting the default template system in django, so it really is worthwhile and I believe it will make you more productive in the long run. )
You can read this article written by its author, although it’s technical, he mentions the problem of the {% cycle %} tag in django.
Jinja doesn’t have a cycle tag, it has a cycle method on the loop:
{% for user in users %}
<li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>
{% endfor %}
A major advantage of Jinja2 is that it allows you to use logic for the presentation, so if you have a list of pictures, you can put them in a table, because you can start a new row inside a table every N elements, see, you can do for example:
{% if loop.index is divisibleby(5) %}
</tr>
{% if not loop.last %}
<tr>
{% endif %}
{% endif %}
you can also use mathematical expressions:
{% if x > 10 %}
and you can access your python functions directly (but some setup is required to specify which functions should be exposed for the template)
{% for item in normal_python_function_that_returns_a_query_or_a_list() %}
even set variables ..
{% set variable_name = function_that_returns_an_object_or_something() %}
- [Django]-Creating Custom Filters for list_filter in Django Admin
- [Django]-Django REST Framework – 405 METHOD NOT ALLOWED using SimpleRouter
- [Django]-Django Installed Apps Location
3👍
You can use tagged cycle
and resetcycle
(new in Django 1.11) calls (from https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#std:templatetag-resetcycle ):
{% for blog in blogs %}
{% cycle 'odd' 'even' as rowcolors silent %}
{% resetcycle rowcolors %}
{% for entry in blog.entries %}
{% cycle rowcolors %}
<div class="{{ rowcolors }}" id="{{entry.id}}">
{{ entry.text }}
</div>
{% endfor %}
{% endfor %}
- [Django]-Django error when installing Graphite – settings.DATABASES is improperly configured. Please supply the ENGINE value
- [Django]-Django: How to manage development and production settings?
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
2👍
I end up doing so, with the forloop.counter0 – It works great!
{% for product in products %}
{% if forloop.counter0|divisibleby:4 %}<div class="clear"></div>{% endif %}
<div class="product {% if forloop.counter0|divisibleby:4 %}col{% else %}col20{% endif %}">
Lorem Ipsum is simply dummy text
</div>
{% endfor %}
- [Django]-How to spread django unit tests over multiple files?
- [Django]-Gunicorn, no module named 'myproject
- [Django]-Django storages aws s3 delete file from model record
1👍
The easiest answer might be: “give up and use jQuery.” If that’s acceptable it’s probably easier than fighting with Django’s templates over something so simple.
- [Django]-Django admin default filter
- [Django]-Remove default apps from Django-admin
- [Django]-New url format in Django 1.9
-5👍
There’s a way to do it server-side with an iterator that doesn’t keep a simultaneous copy of all the entries:
import itertools
return render_to_response('template.html',
{
"flattened_entries": itertools.chain(*(blog.entries for blog in blogs)),
})
- [Django]-Django 1.8: Create initial migrations for existing schema
- [Django]-Django gives Bad Request (400) when DEBUG = False
- [Django]-Django TemplateSyntaxError – 'staticfiles' is not a registered tag library