1

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

Community
  • 1
  • 1
Beginner
  • 2,643
  • 9
  • 33
  • 51
  • did you look at [this answer](http://stackoverflow.com/questions/6157101/django-tables2-linkcolumn-accessor?rq=1) – letsc May 12 '16 at 21:43
  • I did as soon as I posted my question. However since I not rendering a table from a `model` i dont have a use for the `primary key`, also my URL has 2 variables.. – Beginner May 12 '16 at 21:44

0 Answers0