Nested for loop in django template for variables - django

<tbody>
{% for j in dat %}
<tr>
{%for h in hed %}
<td>{{j.h}}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
want to get value by object.field_name from the model. I've tried {{ j }}.{{ h }} but it does not return the value instead returns obj1.field_name. Any possible method i should try.

Related

Jinja2 setting and using variables

I have the following
<thead>
<tr>
<th>Name</th>
{% if datas|length > 0 %}
{% set longest = [] %}
{% for data in datas %}
{% if data['numbers']|length > longest|length %}
{% set longest = data['numbers'] %}
{% endif %}
{% endfor %}
{% for i in longest %}
<th></th>
{% endfor %}
{% endif %}
</tr>
</thead>
I am trying to make enough headers to accommodate the longest list of numbers in the datas dictionary.
Can anyone see what I am doing wrong?
Chris

Django HTML template table rendering

I want to render a table look like this:
<table>
<tr>
<td>some data</td>
<th>special one</th>
<td>some data</td>
...
<td>some data</td>
</tr>
...
</table>
There is a solution that can render all in the same tag.
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
<td>{{val}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
But in my case, there would be a th at the second place for every row of the data, if there is a record.
There is another solution that not as good as the answer below as it keeps partial table, td and tr in the view.
Is there a way to implement this feature?
There are some variables available inside a django template for loop, one of them is named forloop.counter which gives you the current iteration of the loop. You can use this variable to render something differently on the second loop
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
{% if forloop.counter == 2 %}
<th>{{ val }}</th>
{% else %}
<td>{{ val }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>

Django template integer value iteration

I have the following model
class Table(models.Model):
# Some not important attrs
rows = IntegerField(etc)
cols = IntegerField(etc)
Then I have my view where I'm rendering objects of this model. And I need to build some HTML tables based on the quantity of each objects' rows and cols.
View:
get_tables(request):
tables = Table.objects.all()
return render(request, 'tables.html', {'tables': tables})
I'm trying to do something like:
{% for table in tables %}
<table>
<thead>
<tr>
{% for col in table.cols %}
<th>column label here</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in table.rows %}
<tr>my row</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
I know it is possible to loop for key in dict. But the values cols and rows are integers. How can I achieve this on a Django template?
Try
{% for table in tables %}
<table>
<thead>
<tr>
{% with ''|center:table.cols as range %}
{% for _ in range %}
<th>column label here</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% with ''|center:table.rows as range %}
{% for _ in range %}
<tr>my row</tr>
{% endfor %}
{% endwith %}
</tbody>
</table>
{% endfor %}
# You can take use of filter tags in django template
# For Example
Step 1:- Create 'templatetags' package in your app folder.
Step 2:- Create filter.py in 'templatetags' package
Step 3:-
from django import template
register = template.Library()
def table_rows(value):
value_list = [value]
html_row = ''
for val in value_list:
html_row += '<tr></tr>'
return html_row
def table_cols(value):
value_list = [value]
html_cols = ''
for val in value_list:
html_cols += '<td>Hello</td>'
return html_cols
register.filter('table_rows', table_rows)
register.filter('table_cols', table_cols)
Step 4:-
# Your template can be:-
{% load filter %}
{% for table in tables %}
<table border="1">
{% autoescape off %}
{{table.rows|table_rows}}
{{table.cols|table_cols}}
{% endautoescape %}
</table>
{% endfor %}
# You can make changes according to your requirement

Fixing ManagementForm-data

I have some code which looks like this:
<table>
{% for n in model %}
{% for i in formset.forms %}
{% if forloop.parentloop.counter == forloop.counter %}
<tr>
<th>{{ n }}</th>
{% for j in i %}
<th>{{ j }}</th>
{% endfor %}
</tr>
{% endif %}
{% endfor %}
{% endfor %}
</table>
I really want to keep it this way, because this is the presentation I've been asked for. However, I keep getting the "ManagementForm-data missing or tampered with" error, obviously because I'm messing with the formset.
Is there a smart way to fix the managementform-data so my POST will go through, or do I have to reformat my template completely?
(Yes, I am aware that my code contains an ugly, inefficient hack. Please feel free to suggest an alternative, but performance doesn't matter.)
You get the error about missing management form data because you haven't included the management form with {{ formset.management_form }}. See the docs for more info.
To prevent the double loop in the template, you can zip model and formset.forms in the view:
models_and_forms = zip(model, formset.forms)
Then loop through the models_and_forms in the template:
<table>
{{ formset.management_form }}
{% for n, i in models_and_forms %}
<tr>
<th>{{ n }}</th>
{% for j in i %}
<th>{{ j }}</th>
{% endfor %}
</tr>
{% endfor %}
</table>

Custom formset templates in Django

I am using a Django formset for this model:
class Book(models.Model):
book_id=models.AutoField(primary_key=True,unique=True)
book_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
The formset is defined thus:
BookFormset = inlineformset_factory(Author, Book,
fields=('book_id','book_name', 'publisher_name'), extra=1,
can_delete=False)
The template is:
{{ formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
The fields are displayed column-wise, but I would like them to be displayed row-wise.
The above code produces output like this:
Book name Publisher name
book_field Publisher_field
I would like the output to look like this:
Book name book_field
Publisher name Publisher_field
How can I do this?
In your template, you have two <tr> elements, each of which contains a loop over form.visible_fields, each iteration of which generates a single <th> or <td>.
Change this round so that you have a single loop over form.visible_fields, each iteration of which contains a single <tr> element containing a <th> and a <td>. Like this:
<table id="formset" class="form">
{% for form in formset.forms %}
{% for field in form.visible_fields %}
<tr class="{% cycle row1,row2 %}">
<th>{{ field.label|capfirst }}</th>
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
</tr>
{% endfor %}
{% endfor %}
</table>
The examples above seem to show a column-wise layout, which appears to be the default layout when a formset renders itself.
To make it row-wise, use something like this:
<table>
{% for form in formset.forms %}
{% if forloop.first %}
<thead>
{% for field in form.visible_fields %}
<th>{{ field.label }}</th>
{% endfor %}
</thead>
<tbody>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% if forloop.last %}
</tbody>
{% endif %}
{% endfor %}
</table>