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 %}
Related
I have an inlineformset_factory: PollFormset = inlineformset_factory(Post, Poll, form=PollForm, max_num=10, extra=1, can_delete=False) and I want below the form the option to add and create another Poll like in the django admin panel,
template:
{% for form in poll_form %}
{{ form.non_field_errors }}
<div class="container" id="poll_form">
<div class="row" name="service_form">
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form %}
<div class="col-sm">
{{ field.errors }}
{{ field|as_crispy_field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
I also tried {{poll_form.management_form}} but it doesnt work.
The challenge is to reduce the construction and, if possible, speed up the processing of the code.
{% for tp1 in test.tab_tp1s %}
{{ tp1.name }}
{% endfor %}
{% for tp2 in test.tab_tp2s %}
{{ tp2.name }}
{% endfor %}
{% for tp3 in test.tab_tp3s %}
{{ tp3.name }}
{% endfor %}
{% for tab in test %}
{% for tp in test[tab]
{{ tp.name }}
{% endfor %}
{% endfor %}
Maybe, it's hard to tell because we don't know what is stored in test, I'm assuming test is a dictionary holding 3 keys which each hold a sub key called name?
This is my code:
<!-- Loop thru Table Macro begins-->
{% macro loopThruTable(currentParent, prod)%}
<li> {{prod.category_name}} </li>
{% set currentParent = prod.id %}
{{ checkParent(currentParent) }}
<ul>
<table>
<tr>
<th>Service</th>
<th>Price</th>
</tr>
{{ checkParentService(currentParent)}}
</table>
</ul>
{% endmacro %}
<!-- Loop thru Table Macro ends-->
<!-- CheckParent Macro begin-->
{% macro checkParent(currentParent) %}
{% for prod in myProd %}
{% if prod.parent_category_id == currentParent %}
<ul>
{{ loopThruTable(currentParent, prod) }}
</ul>
{% endif %}
{% endfor %}
{% endmacro %}
<!-- checkParent Macro Ends-->
<!-- CheckParentService Macro begin-->
{% macro checkParentService(currentParent) %}
{% for serv in myServ %}
{% if serv.parent_category_id == currentParent %}
<tr>
{{ loopThruTableService(currentParent, serv.name) }}
{{ loopThruTableService(currentParent, serv.price) }}
</tr>
{% endif %}
{% endfor %}
{% endmacro %}
<!-- checkParent Macro Ends-->
<!-- Loop thru Table Macro begins-->
{% macro loopThruTableService(currentParent, blah) %}
<td>{{blah}}</td>
{% endmacro %}
<!-- Loop thru Table Macro ends-->
{% extends 'admin/master.html' %}
{% block body %}
<ul>
{% set parent = 0 %}
{{ checkParent(parent) }}
</ul>
{% endblock %}
for all intents and purposes it is running mostly correctly, but it is adding an extra table at the end of each loop.
enter image description here
as you can see in the bottom, there's a "ServicePrice" that is separating each category.
enter image description here
here's another example. I know it has to do with my loops, but as far as jinja2 is concerned, I don't know if there's a method that exists where i check if the parent just 'exists' in my object.
Thanks,
I figured it out.
I had to add one extra for loop and if statement in the loopThruTable Macro.
It now looks like this:
{% macro loopThruTable(currentParent, prod)%}
<li> {{prod.category_name}} </li>
{% set currentParent = prod.id %}
{{ checkParent(currentParent) }}
{% for serv in myServ %}
{% if serv.parent_category_id == currentParent %}
<table>
<tr>
<th>Service</th>
<th>Price</th>
</tr>
{{ checkParentService(currentParent)}}
</table>
{% endif %}
{% endfor %}
{% endmacro %}
I'm still not entirely sure why that works. But it works.
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.
I have a simple for loop in Django, outputting a series of teaser stories. Each is wrapped in a div with a class of row. I have an varibale called num_of_rows, that adds a class of hidden after 2 loops, which hides these divs from view with css.
Here's my code:
{% block content %}
<h1>{{ section.title }}</h1>
{% for story in story_list %}
<div class="row {% if num_of_rows > 2 %} hidden{% endif %}">
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
</div>
{% endfor %}
{% endblock %}
What I'd like to do is, instead of adding a class of hidden to each individual row, wrap all of the items after 2 items in a separate div and then hide with CSS from that, using Django. This way I can create a much smoother slide-down effect with jQuery.
You can use the forloop variables for this:
{% for story in story_list %}
{% if forloop.counter == 3 %}<div class="hidden">{% endif %}
<div class="row">
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
</div>
{% if forloop.counter > 2 and forloop.last %}</div>{% endif %}
{% endfor %}