django-tables2 linkColumn accessor

2019-06-06 16:37发布

问题:

I have been using django-tables2 which I like, but i run into some problems

I am trying to make a table in which cells link out to a different table, or an outside link the example in the documentation is :

models.py

class Person(models.Model):
    name = models.CharField(max_length=200)

urls.py

urlpatterns = patterns('',
    url('people/(\d+)/', views.people_detail, name='people_detail')
)

tables.py

from django_tables.utils import A  # alias for Accessor

class PeopleTable(tables.Table):
    name = tables.LinkColumn('people_detail', args=[A('pk')])

I have been trying to use this to no success... What would be the view and template that would go with this example? I think there might be a problem with the url but I am not sure what it is... Can anyone explain: args=[A('pk')]

回答1:

args=[A('pk')] is the primary key of the model from which you're displaying the table. Your example would create a column 'Name' with the cell content <a href="/people/pk"></a> pk would be the primary key (number). The view would be views.people_detail and the template would be whatever you've defined in this view...

Here's the link to the doc: django-tables2 doc

Hope this helps...