-->

如何从Django_tables2行的信息?(How to get information from

2019-06-25 01:51发布

我宣布一个表,要取的是使用CheckBoxField字段检查行的价值。 任何帮助,我怎么能在我的意见,使每次我选择一行并点击提交按钮写这种情况下,它返回该行的values.Code是这样的:

    class mytables(tables.Table):
          new_database = tables.CheckBoxColumn()
          student =tables.Column(accessor='Student')
          Class = tables.Column(accessor='class')

而在我的模板,一个提交按钮。

Answer 1:

你需要选择的合适值CheckBoxColumn 。 一般来说,如果你显示的查询集,您将使用pk每个对象为的CheckBoxColumn 。 在你的情况,这将是这样的:

class EnrollmentTable(tables.Table):
    selection = tables.CheckBoxColumn(accessor='pk')
    student = tables.Column()
    class = tables.Column()

然后,你需要渲染表单中的数表,从而使用户可以提交形式,例如:

<form action="/someurl/" method="post">
    {% load render_tables from django_tables2 %}
    {% render_table table %}
    <input type="submit"/>
</form>

然后,你需要挂接到一个视图/someurl/ 。 在你的情况下,将视需要看看POST变量selection

def someview(request):
    if request.method == "POST":
        pks = request.POST.getlist("selection")
        selected_objects = SomeModel.objects.filter(pk__in=pks)
        # do something with selected_objects
    else:
        # ...


文章来源: How to get information from Django_tables2 row?