Django HTML template table rendering - django

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>

Related

overriding Django change_list_results

I am customizing my admin panel a little and want to add an additional column to it, so as per the docs I have to override the change_form_results.html file but how do I do it? all I see is this code in it
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td> </tr>
{% endif %}
<tr>{% for item in result %}
{{ item }}
{% endfor %}</tr>
{% endfor %}
</tbody>
what change am i suppose to make to above code

Django template - How to match model datefield with a date in date generator?

In my models.py Task can have multiple Employer so I use ManyToManyField
In my views.py I got a date generator - it return range of 2 weeks from current day.
So here is my template
<table>
<thead>
<tr>
<th>{% trans 'employer_id' %}</th>
{% for i in date_range %}
<th>{{ i }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for employer in employers %}
<tr>
<td>{{ employer.employer_id }}</td>
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
What I need is when I create a Task and assign it to employers, it should show me in the html table a start_date of Task of each employers for specific days.
So far I got this with codes above:
^date of tasks of these employers are not matching with days in the table header.
Instead of
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
you need to iterate over the dates again, since you're not creating <td>'s for each date.
{% for i in date_range %}
{% for t in employer.task_set.all %}
{% if t.start_date == i %}
<td>{{ t }}</td>
{% else %}
<td>-</td>
{% endif %}
{% empty %}
<td>0</td>
{% endfor %}
{% endfor %}

Return an extra argument with the datachart

I'm trying to print a chart and to return an argument with the datachart to the template.
Views.py:
def errors(request):
totalErrors = total_errors()
table = table_errors()
return render_to_response('macaco_errores.djhtml', {'totalErrors': totalErrors, 'table': table})
There's no problem to print the chart without the 'table' argument, but with both I get 'False' in template when I load in "load_chart".
EDIT:
Template:
{% block head %}
{% load nvd3_tags %}
{% include_chart_jscss %}
{% load_chart charttype totalesData totalesContainer extra %}
{% endblock %}
{% block body %}
<div align="center">
<h1>Errors</h1>
{% include_container totalesContainer 600 1000 %}
</div>
<div align="center">
<table border="1">
<tr>
<th>Log</th>
<th>Type</th>
</tr>
{% for type,log,date in table.items %}
<tr>
<td>{{ type }}</td>
<td>{{ log }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
{% load_chart charttype totalesData totalesContainer extra %}
This tag is needing four variables in the context: charttype, totalesData, totalesContainer, extra
It appears you are not passing any of them from your view function (which provides only totalErrors and table)
So there is no way that this can work from the code you've posted.
See this example from the django-nvd3 docs for the type of context data you need to provide from your view function:
http://django-nvd3.readthedocs.org/en/latest/introduction.html#example-how-to-create-a-piechart
Please see also the Django docs for how to pass variables from the view function into the template for rendering.
For example in your view you are currently using the render_to_response helper which takes a dict of variable names and values to pass to the template... these are the only vars which are available in the template.
The solution was:
def counterroresmacaco(request):
errores = errores_macaco()
errores['tabla'] = tipoerror_ticker()
return render_to_response('macaco_errores.djhtml', errores)
'errores' is a dictionary, then in the template I use a for to iterate over 'tabla'.
The problem to pass two arguments (as I was doing) is that django-nvd3 doesn't recognize the arguments like in the examples.
{% load nvd3_tags %}
{% include_chart_jscss %}
{% load_chart charttype totalesData totalesContainer extra %}
{% include_container totalesContainer 600 1000 %}
<table border="1">
<tr>
<th>Tipo de log</th>
<th>Ticker</th>
</tr>
{% for reg in tabla %}
<tr>
<td>{{ reg.log }}</td>
<td>{{ reg.ticker }}</td>
</tr>
{% endfor %}
</table>

django template view nested list

i made a nested list and from that id like to make a table in my template
list looks something like
ground_skills_available = [[category1, [skill1, skill2, skill3]], [category1, [skill1, skill2]]]
Now i want to list it that you got category with below the items, next category and other items. Problem is, i have no clue about hot to show only category, instead of category +all items, since you cant seem to use the index of a list?
Can someone please help me out?
<table>
{% for categories in ground_skills_available %}
{% for category in categories %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
Change your outer loop to
{% for category, skills in ground_skills_available %}
This technique is described in the Django for loop docs.
On the first iteration of the loop, this will take the list [category1, [skill1, skill2, skill3], and assign
category = category1
skills = [skill1, skill2, skill3]
You can then display {{ category }}, and loop through skills.
Putting that together, you have:
<table>
{% for category, skills in ground_skills_available %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in skills %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% if forloop.index == 1 %} ... some code ... {% endif %}
or
{% if forloop.index0 == 0 %} ... some code ... {% endif %}
Expectation:
{% for categories in ground_skills_available %}
{% for category in categories %}
{% if forloop.first %}
<tr>
<td>{{ category }}</td>
</tr>
{% else %}
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

for loop django template logic - how can I do this?

I am trying to output 2 items for each row. I have 4 items coming from db.
<table>
<tr>
{% for item in items %}
<td>
{{item.name}},{{item.size}}
</td>
{% endfor %}
</tr>
</table>
this is giving me
name1, 23m^2 | name2,20m^2 | name3,15m^2 | name4,10m^2
but i need
name1, 23m^2 | name2,20m^2
name3,15m^2 | name4,10m^2
each row being contained in separate <tr>. I am stuck how to break the loop and assign new row..
Just switch the <tr> and forloop, and also use forloop.counter and divisibleby
Something like this:
{% if items %}
<tr>
{% for item in items %}
<td>{{item.name}},{{item.size}}</td>
{% if forloop.counter|divisibleby:2 %}
</tr>
<tr>
{% endif %}
{% endfor %}
</tr>
{% endif %}
You forgot to close <td> tag.
<table>
<tr>
{% for item in items %}
<td>
{{item.name}},{{item.size}}
</td> <!-- here -->
{% endfor %}
</tr>
</table>