Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer able to manually reference column values when iterating through rows in a template #485

Closed
genghisken opened this issue Oct 9, 2017 · 8 comments · Fixed by drummonds/bene#78 or rlmv/doc-trips#111

Comments

@genghisken
Copy link

Hi

I came to django_tables2 via django_tables.

There are a few occasions when I want to manually iterate through the columns of each row of a table in my template. I've always done this using (e.g. for the det_id column):

{% for row in table.rows %}{{ row.det_id }}
{% endfor %}

It's worked OK in my code for years - on Django 1.9 and django_tables2 1.1.1

However, I recently upgraded to django_tables2 1.10, and this no longer works. (The variable does not exist, but it iterates through the right number of rows.) Can you direct me to the preferred mechanism if I want to manually refer to one of my fields in a template.

@genghisken genghisken changed the title No longer able to manually reference column values when iterating through a rows in a template No longer able to manually reference column values when iterating through rows in a template Oct 9, 2017
@jieter
Copy link
Owner

jieter commented Oct 9, 2017

This is how the supplied template iterates over the rows and columns:

https://github.com/bradleyayers/django-tables2/blob/8cd3470496b564c78ca94298b3588e1e79f5600f/django_tables2/templates/django_tables2/table.html#L24-L38

So I'd say something like this should work:

{% for row in table.rows %}
     {% for column, cell in row.items %}
         {{ column.header }}
     {% endfor %}
{% endfor %}

@genghisken
Copy link
Author

Hi - many thanks for your response. My problem is that I just want to refer a subset of values from a subset of cells. E.g. how do I just print out the values of "det_id", "x" and "y"?

@genghisken
Copy link
Author

I know how to do that in Python, but I need the template syntax. Are you saying that I need to refer to (e.g.) row.record.det_id, row.record.x, row.record.y? This is fine, but it also means that I have to follow the foreign keys from the underlying record. (So why bother with tables at all?) Surely part of the point of creating a table with fields that join more than one underlying model is that I shouldn't need to reference them explicitly via the foreign keys. In django_tables2 1.1.1 I can reference each column of my table. In this case I need the equivalent of whetever calls the get_cell method.

@jieter
Copy link
Owner

jieter commented Oct 9, 2017

I see, I was not thinking your use case through. You are the second one to report that you need to access the keys in templates, so apparently, django-tables2 is useful without having that.

The reason we removed the magic which allows row.column_name was that it did not allow a column named items. We could bring back some property allowing access to cell values, something like row.cell.column_name. I'm open to merge a pull request adding that, if it's well tested.

Orignal bug: #316

@jieter
Copy link
Owner

jieter commented Oct 13, 2017

@genghisken would row.cell.column_name work for you?

@genghisken
Copy link
Author

Yes - that would be great!

@jieter jieter closed this as completed in af2694b Feb 2, 2018
@jieter
Copy link
Owner

jieter commented Feb 2, 2018

After some delay, it's in there: released 1.19.0

table.rows[0].cells.cellname or
table.rows[0].cells['cellname'] or
table.rows[0].cells[idx]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment