我的一个模板中有django_tables2的django数据表。其中一列用于保存指向不同页面的链接。我对SO的问题一直有很多疑问,但似乎没有任何成功。
tables.py
import django_tables2 as tables
from django_tables2.utils import A
class CampaignsTable(tables.Table):
"""docstring for CampaignsTable"""
id = tables.Column(verbose_name='#')
name = tables.LinkColumn('ad_sets', verbose_name='Campaign Name', empty_values=())
# name = tables.Column(verbose_name='Campaign Name')
objective = tables.Column(verbose_name='Campaign Objective')
effective_status = tables.Column(verbose_name='Campaign Effective Status')
class Meta:
attrs = {'class':'table table-striped table-bordered', 'id':'campaigns'}
orderable = True
从上面我尝试将Campaign Name
列转换为LinkColumn
,即每个单元格都被解析为/ad_accounts/{{ ad_account.id }}/campaigns/{{ campaign.id }}/ad_sets
的链接:
urls.py 代码段
url(
r'^ad_accounts/(?P<ad_account_id>[^/]+)/campaigns/(?P<campaign_id>\d+)/$',
CampaignDetailView.as_view(),
name="ad_sets"
根据文档和this answer的建议,我需要在args=[A('pk')]
中添加LinkColumn
,但这样做会给我带来错误
ValueError at /ad_accounts/act_1100111250039687/
Failed lookup for key [pk] in <Campaign> {
"effective_status": "PAUSED",
"id": "6040446448677",
"name": "test77",
"objective": "CANVAS_APP_ENGAGEMENT"
}, when resolving the accessor pk
然后我尝试args=[A('id')]
给了我:
NoReverseMatch at /ad_accounts/act_1100111250039687/
Reverse for 'ad_sets' with arguments '(u'6040446448677',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
我真的很困惑如何在单元格中添加链接以及args=[A('id')]
正在做什么。任何帮助将不胜感激。
更新
刚试过:
name = tables.LinkColumn('ad_sets', args=[A('id')], kwargs={'ad_account.id', 'campaign.id'}, verbose_name='Campaign Name',
empty_values=())
它将我重定向到我的登录页面