【问题标题】:Adding an extra variable to a column in django-tables2在 django-tables2 的列中添加一个额外的变量
【发布时间】:2012-09-24 13:44:42
【问题描述】:

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

中实现

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

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

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

我的代码如下:

TABLES.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",
)

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

VIEWS.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 中的表/列),所以我想知道是否有人知道这样做的简单方法。

谢谢,

埃里克

【问题讨论】:

    标签: django django-class-based-views django-tables2


    【解决方案1】:

    如果您想快速破解,只需实现表的__init__ 方法并将popup 参数动态添加到LinkColumns:

    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
    

    【讨论】:

    • 谢谢布拉德利。我有两个问题;首先是我只需要一些列的额外参数,而不是全部。并非所有列都与接受弹出参数的 reverse() 函数链接;最后一列是删除链接,在模式 DIV 中显示确认页面。此确认页面没有/不需要弹出参数。第二个问题是弹出值似乎被缓存了。如果我从 URL 中删除 popup 关键字,则值为空,但 tables2 仍然显示所有带有“popup”的链接,直到我重新加载服务器。有没有更“hacky”的方式?
    • 我的示例是演示如何在运行时调整列。没有内置的功能可以让你想做的事情变得简单,所以从这个意义上说,任何涉及调整 django-tables2 的解决方案都将是 hacky。您是否考虑过使用查询字符串来指示“弹出”状态?
    • 嗨布拉德利,我有,但这不会给我同样的问题吗?或者有没有办法读取tables2中的查询字符串参数并将其作为参数传递给特定列?
    • 只需修改Table.__init__ 以仅影响您要更改的列...
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 2014-05-21
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2012-02-15
    相关资源
    最近更新 更多