Django 'getattr(row,', expected 'empty' or 'endfor'' - python

Code I'm working with:
{% for row in all_rows %}
<tr>
{% for names in all_fields_names %}
<td> {% getattr(row, names) %} </td>
{% endfor %}
</tr>
{% endfor %}
I get error:
'getattr(row,', expected 'empty' or 'endfor'
I've tried:
{% for row, names in (all_rows, all_fields_names) %}
<tr>
<td> {% getattr(row, names) %} </td>
</tr>
{% endfor %}
With no success. Any ideas?

You can't call functions like that inside the template. You'd want to create a custom template tag.
Custom Template Tag:
#register.simple_tag
def getattr(row, field_name, default=None):
return getattr(row, field_name, default)
Call it inside your template like so:
{% for row, names in (all_rows, all_fields_names) %}
<tr>
<td> {% getattr row names %} </td>
</tr>
{% endfor %}

Related

If statement in table in django-templates

say we have table in template
<table class="table">
<thead>
<tr>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
{% if {{student.academic_status}}=="promoted" %}
<td class=text-success>promoted</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
So is it possible using if statement in table in django-templates
Since you are already in a template tag, you should not use curly brackets ({{ … }}) for the variable, so:
{% if student.academic_status == "promoted" %}
…
{% endif %}
The correct way to add if statement in Django Templates
follow me !
if statement
{% if condition %}
{{here you add the key that you using in the views page exactly context}}
{% endif %}

Django render() not passing variable to template?

def get_queryset(self):
...
#days = {day1 : [[FreetimeStart, FreetimeEnd], [[Meeting1Start, Meeting1End], ...]], ...}
days = {}
for i in range(len(compilation)):
days[daylist[i]] = compilation[i]
print(days)
return render(self.request, 'calendar.html', {"days":days})
Above (with nonrelevant code omitted) is a function from a Django view I've been writing. It's supposed to send the days dictionary over to the template calendar.html (shown below), but it's not- I've confirmed via the print(days) statement that days is being generated properly and that the function is being called. I'm not getting any errors, either.
{{ days }}
<table>
<tr>
{% for key in days %}
<th>
{{ key }}
</th>
{% endfor %}
</tr>
<tr>
{% for value in days.values %}
<td>
{% if value.0 %}
Free time: {{ value.0.0 }} to {{ value.0.1 }}
{% else %}
Free time: None
{% endif %}
</td>
{% endfor %}
</tr>
<tr>
{% for value in days.values %}
<td>
{% for meeting in value.1 %}
Meeting from {{ meeting.0 }} to {{ meeting.1 }}</br>
{% endfor %}
</td>
{% endfor %}
</tr>
</table>
What am I doing wrong here? There doesn't seem to be anything wrong with my render() arguments.

Looping dynamic content on bootstrap4 rows [duplicate]

I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.

How to take at template table one time photo next time string value

I want to make table where second element in every row is photo. Like here: http://www.uefa.com/worldcup/season=2014/standings/
I have all my values in list. In list, third element is photo's url. Other variables are just strings. How to write it in Django template?
Here is my code:
<table>
{% for row in data%}
<tr>
{% for value in row %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
I hope you will understand my problem.
Your {{ value }} for that second element should be a path (relative or absolute) to an image resource. If this data is defined by a model within your Django project, make sure the field is set correctly in models.py--it should be a FileField or ImageField. With those, you get a callable URL property. Then in your template, something along these lines:
<table>
{% for row in data%}
<tr>
{% for value in row %}
{% if value.url %}
<td><img src="{{ value.url }}" alt="..."></td>
{% else %}
<td>{{ value }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>

How to for-loop three columns per row in Django/python?

I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.

Categories