0

尝试构建一个 Django (1.4) 站点,该站点具有一些可以在弹出窗口中加载或不加载的页面。其中一些页面包含一个列表视图,在Django-tables2中实现

当页面加载为弹出窗口时,会添加一个额外的 URL 参数;例如 /backoffice/popup/articlegroups/与 相同的页面/backoffice/articlegroups/,但显示为弹出窗口。

我的问题是如何将这条额外的信息(弹出或不弹出)添加到 Django-tables2 中的 LinkColumns,因为到编辑页面的链接也需要有这些信息。
Django-tables2 有一个访问器,可用于访问查询集中的属性,但我需要在查询集之外添加一条额外的数据。我已经看到向现有数据集添加额外数据充其量是棘手的,而且感觉不是很干净。

我想知道是否没有一种简单的方法可以将额外数据添加到表或列类中,我也尝试查看 table.meta 类,但无济于事。

我的代码如下:

表格.PY

class ArticlegroupTable(tables.Table):

    artg_name = LinkIfAuthorizedColumn(
        'ArticlegroupUpdate',
        args=["popup", A('pk')],
        edit_perm="articles.maintenance",
    )

这当然有效,但它正在将“弹出”参数添加为固定字符串,如您所见......

class ArticlegroupTable(tables.Table):

artg_name = LinkIfAuthorizedColumn(
    'ArticlegroupUpdate',
    args=[A('popup'), A('pk')],
    edit_perm="articles.maintenance",
)

这不起作用因为查询集中没有“弹出”属性......

意见.PY

    def get_context_data(self, ** kwargs):
    # get context data to be passed to the respective templates
    context = super(ArticlegroupSearch, self).get_context_data(**kwargs)
    data = self.get_queryset()
    table = ArticlegroupTable(data, self.request)
    RequestConfig(self.request, paginate={
        "per_page": 5,
        }).configure(table)
    context.update({'table': table})
    if 'popup' in self.kwargs:
        context.update({'popup': self.kwargs['popup']})
    return context

似乎这不是一个非常牵强的场景(将 URL 参数添加到表 2 中的表/列),所以我想知道是否有人知道这样做的简单方法。

谢谢,

埃里克

4

1 回答 1

2

如果您想要快速破解,只需实现表的__init__方法并将popupargLinkColumn动态添加到 s 中:

class ArticlegroupTable(tables.Table):
    def __init__(self, *args, **kwargs):
        if kwargs.pop("popup", False):
            for column in self.base_columns.values():
                if isinstance(column, tables.LinkColumn):
                    column.args.insert(0, "popup")
        super(Table, self).__init__(*args, **kwargs)

    # …

然后在您看来,传入一个popup参数:

def get_context_data(self, ** kwargs):
    # get context data to be passed to the respective templates
    context = super(ArticlegroupSearch, self).get_context_data(**kwargs)
    data = self.get_queryset()
    popup = self.kwargs.get('popup')
    table = ArticlegroupTable(data, self.request, popup=popup)
    RequestConfig(self.request, paginate={
        "per_page": 5,
        }).configure(table)
    context.update({'table': table, 'popup': popup})
    return context
于 2012-09-24T22:52:17.393 回答