Django_tables2:从字典创建表

时间:2016-05-05 21:09:13

标签: django datatables django-tables2

我正在使用django_tables2将我的表格更改为datatables。但是我真的很困惑,我该怎么做呢。我的观点campaigns.py我有这个(这个文件还有另外一个有2种方法的类):

class CampaignListView(FacebookAdInit):
    """ CampaignListView for viewing all the campaigns"""

    def get(self, request, *args, **kwargs):
        ad_account = self.get_ad_account(kwargs.get('ad_account_id'))

        campaigns = self.get_campaigns(ad_account.get('id')) \
            if ad_account else None
        context = {'campaigns': campaigns, 'ad_account': ad_account}

        return render(request, 'app/campaigns/index.html', context)

我使用context HTML标记在我的模板campaigns/index.html中将<table>,<td> and <tr>字典显示为一个表格:

<table class="table table-bordered table-striped" id="campaigns">
    <thead>
    <tr>
        <th> #</th>
        <th> Campaign Name</th>
        <th> Campaign Objective</th>
        <th> Campaign Effective Status</th>
    </tr>
    </thead>
    <tbody>
    {% for campaign in campaigns %}
        <tr>
            <td> {{ forloop.counter }} </td>
            <td>
                <a href="/ad_accounts/{{ ad_account.id }}/campaigns/{{ campaign.id }}/ad_sets">
                    {{ campaign.name }} </a>
            </td>
            <td> {{ campaign.objective }}</td>
            <td> {{ campaign.effective_status }} </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

现在我该怎么做才能使它成为'分页'数据表。我已经经历了无数的SO问题和this one looked helpful,但如何将我的视图导入tables.py,如果我的结构是以下,我在哪里放置tables.py文件:

folder\
 migrations\
 static\
 templates\
  app\
   #a whole bunch of templates
 views\
  campaigns.py
 __init__.py
 models.py
 forms.py
 ...
 ...

1 个答案:

答案 0 :(得分:0)

你似乎没有在你发布的代码中使用django-tables2:你正在运行自己的表模板,这很好,但不是使用django-tables2的预期方式。

Django-table2也不是要向datatables-instance提供数据。它可能有用,但它打算生成静态html表。

相关问题