Django print loop index inside List - django

I am trying to print the list of values from a list. This code works:
<td id="b0">{{data.0.B}}</td>
<td id="b1">{{data.1.B}}</td>
<td id="b2">{{data.2.B}}</td>
<td id="b3">{{data.3.B}}</td>
I am trying to put this in a loop like so:
{% for i in data%}
<td id ='b{{forloop.counter0}}'>
Data is{{ data.forloop.counter0.B }}
</td>
{% endfor %}
This prints the td id as b0, b1 etc correctly. However it is unable to replace the forloop.counter0 inside data.forloop.counter0.B and is not printing it as data.0.B, data.1.B etc.
How do I change the code so that data.forloop.counter0.B is printed in the loop as data.0.B, data.1.B etc?

try following
{% for i in data%}
<td id ='b{{forloop.counter0}}'>
Data is{{ i.B }}
</td>
{% endfor %}

Related

How to add two numbers in jinja

I have a mark object which I want to list in table
{% for mark in marks %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{mark.subject.name}}</td>
<td>75</td>
<td>25</td>
<td>{{mark.mark}}</td>
<td>{{mark.mark_pr}}</td>
<td>{{ (mark.mark + mark.mark_pr) }}</td>
<td>A</td>
<td>70</td>
</tr>
{% endfor %}
I want to display the sum of mark.mark and mark.mark_pr into third last but when I try doing
(mark.mark + mark.mark_pr) it gives me error saying "Could not parse the remainder: '(mark.mark + mark.mark_pr)' from '(mark.mark + mark.mark_pr)" do anyone have any idea on how to do it?
Thanks in advance.

queryset update with multiple variables duplicate

I am having an issue that I have database updated with the same value as for both id's even though they are different as per my example below:
Lets assume that I entered as input for id 1 -- P_350 =" try_1" id 2 -- P_350 = "try_2" when I click submit then it become both id's the same "try_2" value.
What you suggest to overcome this situation.
Should I need to use variable input name including the id in front of the input name ?
Or any easier method ?
I have the below view.py for my database updates:
ids = request.POST.getlist("id")
qs = fp.objects.filter(id__in=ids)
P_350 = request.POST["P_350"]
P_450 = request.POST["P_450"]
updates = {}
if len(P_350) > 1:
updates['P_350'] = P_350
if len(P_450) > 1:
updates['P_450'] = P_450
if updates:
qs.update(**updates)
Here is my html file input section:
<td style="display:none;">
<input name="id" type="text" value={{ field.id }} >
</td>
<td width="650">
{{ field.FP_Item }}
</td>
{% if field.P_350|length == 0 %}
<td style="display:none;">
{% else %}
<td>
{% endif %}
<input name="P_350" type="text" value={{ field.P_350 }} >
</td>
{% if field.P_450|length == 0 %}
<td style="display:none;">
{% else %}
<td>
{% endif %}
<input name="P_450" type="text" value={{ field.P_450 }} >
</td>
Wrong logic
If you putting different value at same input P_350 this will get the last value you place when it reaches the backend, you probabily should put your first value in P_350 and the second in P_450.
In your example you only manipulate P_350 2 times... so P_450 will never be updated because he dont have a new value.
If you have multiple inputs related with P_350, you should first get all first and then send to backend (AJAX like async) or place multiple P_350 with different names and handle it at backend when save each one
https://medium.com/#taranjeet/adding-forms-dynamically-to-a-django-formset-375f1090c2b0

Nested table rows in Vue

There has been several versions of this question, but I've found a specific scenario I can't get my head around. I have this template on a parent element:
<tbody>
<tr is="tree-item" v-for="item in children" :item="item"></tr>
</tbody>
So far so good. The child element is:
<tr v-on:click="toggle" class="{{ classes }}">
<td class="name">
{{ item.tree_item_heading }}
</td>
</tr>
<tr v-show="isLoaded" is="tree-item" v-for="item in grandChildren" :item="item"></tr>
It's a recursive form line, so if the first tree-item has children, they will render as tree-item too. Although it shows up fine, it is rendered as a Fragment Instance, hence the v-show property gets ignored.
Any idea on how to solve this?
Cheers
You could try using multiple tbody tags for your parent loop:
<tbody v-for='item in children'>
<tr is="tree-item" :item="item"></tr>
<tr v-show="isLoaded" is="tree-item" v-for="gItem in item.children" :item="gItem"></tr>
</tbody>

Display table data django

I want to display data from a table in a table; to retrieve the object I used the foreign key to retrieve the object (person depending on the project)
I have two tables: person & project.
class person (models.Model)
project =models.ForeignKey('plat_project.Project', blank=True,related_name='project')
pourcentage = models.IntegerField()
sum = models.IntegerField()
class project
...
in the views :
in the display function :
PersonneProject=PersonneProjet.objects.all(projet=project)
response['Listeperson']=PersonneProject
And the template :
{% for p in Listeperson %}
<tr>
<td style="text-align:center">{{ i }} </td>
<td style="text-align:center">{{ p.pourcentage}}% </td>
<td style="text-align:center">{{ p.sum }}€ </td>
</tr>
{% endfor %}
I have two problems:
I can not display the data on my chart
How to increment the i 1
You'll need to provide more information about your view function to fix the first problem (how and what are you returning from this function? Note also your typo in projet=project), but you get the loop counter with forloop.counter:
<td style="text-align:center">{{ forloop.counter }} </td>
(or forloop.counter0 if you want zero-based indexes).

Get parent id in django-mptt

I am using django-mptt and jquery-treetable.
I am printing my objects with:
<table>
{% for node in nodes %}
<tr>
<td>{{ node }}</td>
</tr>
{% endfor %}
</table>
In jquery-treetable the <tr> element should have some attributes to identify which rows are children of which rows.
It needs to have the following setup
<table>
<tr data-tt-id="1">
<td>Parent</td>
</tr>
<tr data-tt-id="2" data-tt-parent-id="1">
<td>Child</td>
</tr>
</table>
but I can't seem to find the right template variables to identify the children correctly. I have only found node.id, node.tree_id, node.level, node.lft, and node.rght.
If your nodes are MPTTModels then you should have a 'parent' relationship to 'self'. Assuming that is the case, you should be able to get the parent id by doing:
node.parent.id