【问题标题】:Django_tables2: Create table from dictionaryDjango_tables2:从字典创建表
【发布时间】:2016-05-05 21:09:12
【问题描述】:

我正在使用django_tables2 将我的表格更改为datatables。但是,我真的很困惑如何继续进行此操作。我的观点 campaigns.py 我有这个(这个文件有另外 1 个其他类和 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 字典显示为表格:campaigns/index.html,使用<table>,<td> and <tr> HTML 标签:

<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
 ...
 ...

【问题讨论】:

    标签: django datatables django-tables2


    【解决方案1】:

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

    Django-table2 也不意味着将数据提供给数据表实例。它可能有效,但它旨在生成静态 html 表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2018-11-06
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2018-12-17
      相关资源
      最近更新 更多