I have a django datatable using django_tables2 in one of my templates. One of the column is used to hold links to different pages. Ive been to a lot of questions regarding my issue on SO but cant seem to have any success.
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
From the above Im trying to convert the Campaign Name
column to a LinkColumn
i.e. where each cell is a link resolved as /ad_accounts/{{ ad_account.id }}/campaigns/{{ campaign.id }}/ad_sets
:
urls.py snippet
url(
r'^ad_accounts/(?P<ad_account_id>[^/]+)/campaigns/(?P<campaign_id>\d+)/$',
CampaignDetailView.as_view(),
name="ad_sets"
As suggested in the doc and this answer I need to add args=[A('pk')]
in my LinkColumn
, but doing that gives me the error
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
I then tried args=[A('id')]
which gave me :
NoReverseMatch at /ad_accounts/act_1100111250039687/
Reverse for 'ad_sets' with arguments '(u'6040446448677',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Im really confused as to how to go about adding a link in a cell and what exactly the args=[A('id')]
is doing. Any help will be greatly appreciated.
UPDATE
just tried:
name = tables.LinkColumn('ad_sets', args=[A('id')], kwargs={'ad_account.id', 'campaign.id'}, verbose_name='Campaign Name',
empty_values=())
it redirects me to my login page