(tables) Link to another field

1 view
Skip to first unread message

Kless

unread,
Jan 24, 2009, 12:24:31 PM1/24/09
to Django Apps
I would to get the value of 'id' in the link for each row:
I've tried with: *table.base_columns[value]* but nothing

The idea is that I want to get a link linking to its 'id' but showing
its name

tables:
----------
class Foo(tables.ModelTable):
id = tables.Column(visible=False)
en_name = tables.Column(verbose_name='english name')
fr_name = tables.Column(verbose_name='french name', visible=False)
----------

templates:
----------
{% for row in table.rows %}
<tr>
{% for value in row %}
<td><a href="{{ table.base_columns[value] }}">{{ value }}</
a></td>
{% endfor %}
</tr>
{% endfor %}
----------

Michael Elsdörfer

unread,
Jan 27, 2009, 6:12:46 PM1/27/09
to Django Apps
I'm not 100% sure what you want to accomplish, but generally, when you
have a (bound) row object, you can access specific column values using
the item protocol:

table_instance.rows[x]['id'] # in Python
table_instance.rows.0.id # in Templates

So if you need the id value of the current row to build a link, this
should work:

{% for row in table.rows %}
<tr>
{% for value in row %}
<td><a href="{{ row.id }}">{{ value }}</a></td>
{% endfor %}
</tr>
{% endfor %}

If you need to access a cell with an arbitrary column name, the []
syntax is not supported by Django in templates. You'd have to write a
"getitem" filter (provided there isn't one built-in that I am not
aware of).

Michael

Kless

unread,
Jan 28, 2009, 3:16:29 AM1/28/09
to Django Apps
Thanks! This is just what I wanted.
And I'm sorry! I'm a newbie with django templates.
Reply all
Reply to author
Forward
0 new messages