django tables2 create extra column with links
Asked Answered
S

3

8

I am trying to add a extra column to one of my tables, that adds url to another page.

My Table:

class ItemTable(tables.Table):
    edit = tables.LinkColumn('item_edit', args=[A('pk')])
    class Meta:
        model = Item
        fields = ('name', 'slot', 'klass', 'rarity', 'price')

my urls:

url(r'^admin/item/edit/(?P<item_id>\d+)/$', views.item_edit, name='item_edit')

Now with this, i get my table, but the last column (edit) only has dashes + the page crashes when i click the title.

i have been looking at http://django-tables2.readthedocs.org/en/latest/#django_tables2.columns.LinkColumn and im not sure where i go wrong

Sandhog answered 8/4, 2014 at 15:26 Comment(0)
W
11

The problems you've encountered are caused by LinkColumn expecting to be bound to a specific attribute in your Item model, i.e. it is looking for an Item.edit attribute on your instances.

Since you don't actually have an Item.edit attribute, ordering over your edit column makes no sense, and you should mark it as non-orderable:

from django_tables2.utils import A
edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False)

The text of the link itself would come from the value of the Item.edit attribute, which you don't have, so you will need to provide it yourself by adding a render_edit method to your table class:

def render_edit(self):
    return 'Edit'

You can replace the 'Edit' string with whatever you want displayed in that column.

Update: As suggested by @SunnySydeUp, you also need to specify empty_values=() for the column, in order to get its value rendered:

edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False, empty_values=())

References:
http://django-tables2.readthedocs.org/en/latest/pages/order-by-accessors.html#specifying-alternative-ordering-for-a-column http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html#table-render-foo-methods

Disclaimer: This answer is based on the django-tables2 documentation and source code, and hasn't been tested on an actual Django application.

Weise answered 8/4, 2014 at 15:26 Comment(5)
Thanks for this clear explanation, this indeed solves the problem about the ordering problem. However all my "links" are still just an unclickable "—" any idea where this is going wrong?Sandhog
You also need to add empty_values=() to the column. Look at the 2nd reference.Diley
Yes the values now show, except its not a link yet just a stringSandhog
Can you post your current table class definition, with all discussed changes applied?Weise
The reason it's just a string and not a link is because you have overidden the render method for the edit column to just return a string. You should probably return a string with an anchor tag, eg. "<a href='url'>Edit</a>, but it'll probably be easier just to use a TemplateColumn.Diley
M
6

To have the link properly formatted and with a link text of your choice, you can do the following in the table class:

def render_edit_link(self,record):
    return mark_safe('<a href='+reverse("edit", args=[record.pk])+'>Edit</a>')

Where 'edit' is the name of the url.

Matriarch answered 16/4, 2015 at 21:24 Comment(1)
Also need to mention that LinkColumn text argument needs to be set.Waw
I
3

I create clickable links in extra columns with

edit = tables.LinkColumn('item_edit', text='Edit', args=[A('pk')], \
                         orderable=False, empty_values=())

It's not necessary to override the render method; the 'text' parameter changes the text of the link from 'none' to 'Edit', for example.

Izak answered 26/10, 2016 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.