range in jinja2 inside a for loop - python

I have a nested list. I need to iterate through a list and keep it in for loop as shown below.
{% for alpha in list %}
<div id="{{ loop.index }}">
<div class='sidebar-one'>
{% for beta in list[0][2:] %} #I want to iterate through list[0][2:] till list[n][2:]
<p> {{ beta[0][0] }} </p>
{% endfor %}
</div>
</div>
{% endfor %}
I tried range but no luck.
{% for n in range(1,n) %}
{% for line in check[{{n}}][2:] %}
{% endfor %}
it threw error:
TemplateSyntaxError: expected token ':', got '}'

It's just like Python:
{% for n in range(n) %}
{% for line in check[n][2:] %}
<p> {{ beta[0][0] }} </p>
{% endfor %}
{% endfor %}

You can use the "length" property:
{% for n in range(yourList| length) %}
<p class="someclass">{{n + 1}}.</p>
<a class="someclass2"
href="{{ url_for( 'yourFunction', Int = yourList[n].iterable)}}">
{{yourList[n].iterable}}</a><br>
{% endfor %}
Length is similar to len(yourlist) that we have in python.

Related

Forloop in django template

How could I get a next object in django for loop?
I mean is it possible to do like this {{ route.segments.all.ind }}?
{% for segment in route.segments.all %}
{% if segment.departure == departure %}
{% with forloop.counter as ind %}
<span style="margin-top: -5px; font-size: smaller">
$ {{ route.segments.all.ind }}
{{ segment.distance }}km
{{ segment.duration }}hour</span>
{%endwith %}
{% endif %}
{% endfor %}

Error in the template with Django : can I make arithmetic in if statement

I would like to make a pagination with Bootstrap : a new page every 10 new field in data.
file.html
{% for d in data %}
{% if forloop.first %}
<ul class="pagination">
{% endif %}
{% if (forloop.counter % 10) == 0 %}
<li>{{ forloop.counter % 10 }}</li>
{% endif %}
{% if forloop.last %}
</ul>
{% endif %}
{% endfor %}
output I would like that => Bootstrap pagination
But Django give me an error for this :
{% if (forloop.counter % 10) == 0 %}
TemplateSyntaxError :/
I don't know how to do except create my own filter or add a filter, but i would like to know first if i can do in the template first.
PS: I use Django 1.5 and I can't upgrade it.
Edit:
Finally I use this condition:
{% if forloop.counter|divisibleby:'10' and forloop.counter|divisibleby:'5' and forloop.counter|divisibleby:'2' %}
Like that I know when I have a 10 multiple.
The modulus (%) operator is not available in django templates. However, you can use the divisibleby (https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#divisibleby) template filter, something like
{% if forloop.counter|divisibleby:"2" %}
Use the paginator, your QuerySet are not evaluated for the hole table, just the number you need to build the page, and it offers properties that you can use in the template like (page_range, next_page_number, has_next, etc.)
here is the code withe BootStrap 2 and django.core.paginator:
<div class="pagination pagination-centered">
<ul>
{% if MYDATAENTIRES.has_previous %}
<li>
{% trans "Précédent" %}
</li>
{% endif %}
{% for i in MYDATAENTIRES.paginator.page_range %}
<li {% ifequal MYDATAENTIRES.number i %} {{ 'class="disabled"' }} {% endifequal %}>
<a href="?page={{ i }}">
{{ i }}
</a>
</li>
{% endfor %}
{% if MYDATAENTIRES.has_next %}
<li>
{% trans "Suivant" %}
</li>
{% endif %}
</ul>
</div>

Django template remove sub list

Maybe a stupid question but how do I remove a sublist from list in django template
I have something like this:
{% for j in sub_com|slice:"1" %}
{% for k in j|slice:"3" %}
<li> {{k}} </li>
{% endfor %}
{# remove sublist from list here #}
{% endfor %}
You can probably use templateContext.pop here.
{% for j in sub_com|slice:"1" %}
{% for k in j|slice:"3" %}
<li> {{k}} </li>
{% endfor %}
{{ j.pop.0 }}
{% endfor %}

How to get only first object in template?

views:
....
d = Data.objects.filter(is_accepted=True)
....
templates:
{% for item in d %}
{% for picture in item.photo_set.all %}
<img class="image" src="{{ picture.photo.url}}">
{% endfor %}
{% endfor %}
How to get only first photo? ({{ picture.photo.url}})
You can index with the dot notation:
{{ item.photo_set.all.0.photo.url }}
You need to use forloop.first. Have a look at the docs.
{% for item in d %}
{% for picture in item.photo_set.all %}
{% if forloop.first %}
<img class="image" src="{{ picture.photo.url}}">
{% endif %}
{% endfor %}
{% endfor %}
First, a sharp knife.
Second, a slice

Django {% if forloop.first %} question

I have the following code in my template:
{% for object in object_list %}
{% with game=object.game %}
{% for category in object.game.objectmeta.categories.all %}
{% if category.name|title == 'Puzzle' %}
{% if forloop.first %}
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% endif %}
<div class='game_link' id='dark_gamelink'>
<a class='img_link' href="{% url game_view game.id game.title|slugify %}">
<img class='game_img' src='{{game|thumb:"78x65"}}' alt='{{game.title}}' />
</a>
<div class='top_game_title' style='padding:0'>
<a style='position:relative; top:-3px' id='yellowlink' href="{% url game_view game.id game.title|slugify %}">{{game.title}} -- {{category.name|title}}</a>
<img style='position:relative; top:1px; margin-left:12px' src='thumbsup.gif' width='17' height='18'/>
<span style='position:relative; top:-3px; font-size:10px; color:white'>99%</span>
</div>
{% if game.description|length > 65 %}
{{ game.description|slice:"65" }}...
{% else %}
{{ game.description }}
{% endif %}
</div>
{% if forloop.counter0 == 3 %}
<div class='more_games'><br/></div><div class='side_header' id='dark_gamelink_side'><a class='adventureheader' href=adventure.htm></a></div>
{% endif %}
{% endif %}
{%endfor%}
{% endwith %}
{% endfor %}
Now I'm using this:
{% if forloop.first %}
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% endif %}
to try to detect if this is the first iteration of the for loop immediately preceding it not the parent forloop. In other words I'm trying to detect if it's the 1st iteration of this for loop:
{% for category in object.game.objectmeta.categories.all %}
not this one:
{% for object in object_list %}
The way it is now isn't working because it's displaying this:
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
Twice. How to detect the first iteration of the nested forloop?
Edited:
I have never used these variables but I think forloop.parentloop.first should do it. If not blame me to have misunderstand the Django docs. ;-)
You should check if you are within the parentloop and and then within the first nested node. Please try this modified template. It should you give the right direction.
{% if forloop.parentloop.first %}
I am in the first loop of the parent
{% else %}
{% if forloop.first %}
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% endif %}
{% endif %}
I think the best way to solve this isn't to detect if this is the first iteration in the loop, but rather to write your HTML so that is outside the loop entirely.
You should only be writing HTML elements in the for loop that you actually want repeated for each iteration. If that doesn't work, rethink how you're providing the data to your view (object_list, game, category, etc) so that you can write your markup more easily.
The beginning of your view will probably look something like this:
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% for object in object_list %}
{% with game=object.game %}
{% for category in object.game.objectmeta.categories.all %}
{% if category.name|title == 'Puzzle' %}

Categories