如何在django表2中为每条记录添加提交按钮?

时间:2014-08-20 22:02:18

标签: django django-tables2

我在表单中有一个django_tables2表,这是必要的,因为顶部有一个删除选中按钮。在每一行中,我都需要一个提交按钮,其中包含“接受”,“拒绝”等操作。

但我不知道如何识别按下该按钮的行。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

我发现这种方法可以为每一行添加一个按钮,其id引用您选择的行:

app / tables.py

import django_tables2 as tables
from app.models import MyModel
from django.utils.safestring import mark_safe
from django.utils.html import escape

class MyColumn(tables.Column): 
    empty_values = list() 
    def render(self, value, record): 
        return mark_safe('<button id="%s" class="btn btn-info">Submit</button>' % escape(record.id))

class MyTable(tables.Table):
    submit = MyColumn()
    class Meta:
        model = MyModel
        ...
app / views.py中的

,就像平常一样:

def mypage(request) :
    table = MyTable(MyModel.objects.all())
    RequestConfig(request,paginate=False).configure(table)
    return render(request,'template.html',locals())

然后我使用Jquery通过检索他们的id来触发我想要的每个按钮......

对于您想要做的事情,这可能有点过于简单,我已经看到其他解决方案可能更有效,使用LinkColumns和Django URL调度程序:Django-Tables2 LinkColumn link doesn't work