繁体   English   中英

如何将数据传递到django_tables2的LinkColumn

[英]How to pass data to django_tables2's LinkColumn

我的urlconf代码段是:

url(r'^play/(?P<songid>\d+)', 'playid', name="playsongid")

playid是一个视图函数,其定义如下:

def playid(request, songid):
    #do something

我使用django-tables2库,其教程为[django-tables2] [1]。 我的表sinppet是:

import django_tables2 as tables

class TestTable(tables.Table)
    links = tables.LinkColumn("playsongid", kwargs={"songid": ...})

“ ...”是我的困惑立场。 如何在我的视图函数中向它传递各种songid数据(此视图函数不是playid ,而是另一个视图函数)? 我希望django-tables2可以呈现如下数据:

column
<a href="/play/1">aaa</a>
<a href="/play/2">bbb</a>
<a href="/play/3">ccc</a>

最终,我让它自己工作! table.py最终像这样:

import django_tables2 as tables
from django_tables2.utils import Accessor

class TestTable(tables.Table)
    songid = tables.Column()
    links = tables.LinkColumn("playsongid", kwargs={"songid": Accessor("songid")})

在我的视图函数中,我像这样将数据传递给它

    playlist = conn.playlistinfo()
    table_data = []
    for info in playlist:
        tmp_dict = {"songid": info.id}
        tmp_dict["links"] = "%s - %s" % (get_attr(info, "artist"), get_attr(info, "title"))
        table_data.append(tmp_dict)
        del tmp_dict
    variables["table"] = PlaylistTable(table_data)  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM