如何将 link 添加到自定义布尔字段并使用 Django_Tables2 传递参数

How do I add a link to a Custom Boolean Field and pass parameters using Django_Tables2

所以我在 Django_Tables2 上玩得很开心,而且我的所有工作都非常好,这很棒。我的 table 使用数据库中的一系列布尔值列进行渲染。这些是 'Completed' 等列。我没有为 True 和 False 创建一个自定义定义我的布尔字段,它根据需要呈现 glyphicons-ok 和 glyphicons-remove。请参阅下面的代码

class BootstrapBooleanColumn(BooleanColumn):
    def __init__(self, null=False, **kwargs):
        if null:
            kwargs["empty_values"]=()
        super(BooleanColumn, self).__init__(**kwargs)

    def render(self, value):
        value = bool(value)
        html = "<span %s></span>"

        class_name = "glyphicon glyphicon-remove"
        if value:
            class_name = "glyphicon glyphicon-ok"
        attrs={'class': class_name}

        attrs.update(self.attrs.get('span', {}))

        return mark_safe(html % (AttributeDict(attrs).as_html()))

因此,我的专栏相应地编码如下:

completed = BootstrapBooleanColumn(attrs={'th':{'class':'centered nodec'}, 'td':{'data-title':'Completed'}})

但是现在我希望能够单击其中一个图标并让它相应地切换和更新我的数据库(即从 False 切换到 True,反之亦然。)但我似乎做不到能够传递参数,我陷入了困境。

我试着用锚包裹起来

def render(self, value):
    value = bool(value)
    html = "<a href='/tasks/toggle/3'><span %s></span></a>"

这会触发我的 url,硬编码 ID 为“3”,但我不知道如何传递任何可变参数。也许我以错误的方式解决这个问题,所以如果有人能指出正确的方向,我将不胜感激。

所以在费尽周折之后,我设法想出了一个可行的解决方案。它可能不是最迷人的拼凑,但对于我所需要的,它很管用。我将其发布在这里,以防它对其他人有所帮助,如果有人想出更优雅的解决方案,那就太好了。

正如我所怀疑的那样,我最初是找错了树。因此,我没有尝试 subclass 和修改内置的 BooleanColumn,而是决定从另一个角度通过 subclassing 和修改内置的 LinkColumn 来解决它。

下面是我作为 class 添加到我的 tables.py 文件中的代码。

class BoolLinkColumn(LinkColumn):
    def __init__(self, viewname=None, urlconf=None, args=None, kwargs=None, current_app=None, attrs=None, **extra):
        super(LinkColumn, self).__init__(attrs, **extra)
        self.viewname = viewname
        self.urlconf = urlconf
        self.args = args
        self.kwargs = kwargs
        self.current_app = current_app

    def render_link(self, uri, record, value, attrs=None):        
        value = bool(value)  
        column = self.args[1]
        html = "<a href='/tasks/toggle/"+str(record.pk)+"/"+str(column)+"/' %s></a>"
        class_name = "glyphicon glyphicon-remove"
        if value:
            class_name = "glyphicon glyphicon-ok"
        attrs={'class': class_name}       
        attrs.update(self.attrs.get('a', {}))  
        return mark_safe(html % (AttributeDict(attrs).as_html()))

在 def render_link 中,我获取列的值和我在每一列上指定的第二个参数。实际上,这是我单击的列的名称,但我找不到任何其他动态获取列名称的方法。

然后我使用列名和 pk 动态修改预编码的 html 字符串。我将变量通过我的 urls.py 文件传递​​到我的 views.py 文件。来回传递变量对我来说仍然有点困难,所以如果有人能告诉我更好的方法来做到这一点,那就太好了。

然后我将图标的默认值设置为 false,然后测试我单击的列的值。如果值为真,我显然会根据需要更改图标。

最后我更新了我的 a 标签上的属性和 return 完整的 html 渲染字符串。

现在我已准备就绪并开始工作,我可以按如下方式指定我的专栏。

class MyTable(ColumnShiftTable): 
    completed = BoolLinkColumn('my reverse url view', args=[A('pk'),'completed'], text='', empty_values=(), attrs={myattrs}

所以归根结底,这让我能做什么,我为什么要经历这一切?很简单,我正在使用 django_tables2 来呈现我的表格,现在我已经在我的 tables.py 文件中放置了它,我可以指定我选择的任何布尔列以具有 link。只要我在我的 views.py 文件中编写了适当的处理代码,我就可以在我的应用程序中显示一个列表,并且我可以直接从列表中更新(切换)布尔字段,而无需打开编辑 window和从数据库中调用记录等,都是在后台完成,然后屏幕刷新,非常爽。

与所有事情一样,可能有更好的方法来实现这一点,但我在任何地方都找不到任何文档,所以这就是我想出的方法,我对结果非常满意。 :-)

编码愉快!