django-tables2 linkColumn 访问器
我一直在使用django-tables2,我觉得这个工具很好,但我遇到了一些问题。
我想做一个表格,里面的单元格可以链接到另一个表格,或者链接到外部网站。文档中的示例是:
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')])
我一直在尝试使用这个,但没有成功……这个示例需要配合什么样的视图和模板呢?我觉得可能是链接有问题,但我不太确定是什么……有人能解释一下:args=[A('pk')]
吗?
1 个回答
3
args=[A('pk')
是你要显示表格的模型的主键。你的例子会创建一个名为 'Name' 的列,单元格的内容是 <a href="/people/pk"></a>
,其中 pk 就是主键(一个数字)。
这个视图是 views.people_detail
,而模板则是你在这个视图中定义的内容...
这里有文档的链接: django-tables2 文档
希望这对你有帮助...